export class ArrayDataSource {
constructor() {
this.data = [];
}
This class is a simple native array based DataSource implementation The constructor initializes one property:
export class ArrayDataSource {
constructor() {
this.data = [];
}
Sets the user and pass properites. Takes three arguments:
login(user, pass, cb) {
this.user = user;
this.pass = pass;
cb(null, user, pass);
}
Pushes an item into the array as a create function. Takes two arguments
create(item, cb) {
this.data.push(item);
cb(null, item);
}
Updates an item in the array Takes two arguments
update(item, cb) {
for(let i in this.data){
if(this.data[i]._id == item._id){
this.data[i] = item;
cb(null, this.data[i]);
return;
}
}
cb(new Error('Item not found in data.'), item);
}
Removes an item in the array Takes two arguments
remove(id, cb) {
for(let i in this.data){
if(this.data[i]._id == id){
this.data.splice(i, 1);
cb(null, this.data.length);
return;
}
}
cb(new Error('Item not found in data.'), id);
}
Gets an item in the array Takes two arguments
get(id, cb) {
for(let i in this.data){
if(this.data[i]._id == id) {
cb(null, this.data[i]);
return;
}
}
cb(new Error('Item not found in data.'), id);
}
}