-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
64 lines (64 loc) · 1.76 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var Model = (function () {
function Model(storage) {
this.storage = storage;
}
Model.prototype.create = function (title, callback) {
title = title || '';
callback = callback || function () { };
var newItem = {
title: title.trim(),
completed: false
};
this.storage.save(newItem, callback);
};
;
Model.prototype.read = function (query, callback) {
var queryType = typeof query;
callback = callback || function () { };
if (queryType === 'function') {
callback = query;
return this.storage.findAll(callback);
}
else if (queryType === 'string' || queryType === 'number') {
query = parseInt(query, 10);
this.storage.find({ id: query }, callback);
}
else {
this.storage.find(query, callback);
}
};
;
Model.prototype.update = function (id, data, callback) {
this.storage.save(data, callback, id);
};
;
Model.prototype.remove = function (id, callback) {
this.storage.remove(id, callback);
};
;
Model.prototype.removeAll = function (callback) {
this.storage.drop(callback);
};
;
Model.prototype.getCount = function (callback) {
var todos = {
active: 0,
completed: 0,
total: 0
};
this.storage.findAll(function (data) {
data.forEach(function (todo) {
if (todo.completed) {
todos.completed++;
}
else {
todos.active++;
}
todos.total++;
});
callback(todos);
});
};
;
return Model;
}());