-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasklist.js
62 lines (46 loc) · 1.63 KB
/
tasklist.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
$.parse.init({
app_id : "PTB9aRvLMqnKJqB5vpsMVyEnlDAmFENR7JCLjUm4", // <-- enter your Application Id here
rest_key : "PiQH3q4MAatwIJ2NnzZXuJslIcht50D3l4r1Ojns" // <--enter your REST API Key here
});
function Task(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
this.objectId = data.objectId;
}
function TaskListViewModel() {
// Data
var self = this;
self.tasks = ko.observableArray([]);
self.newTaskText = ko.observable();
self.incompleteTasks = ko.computed(function() {
return ko.utils.arrayFilter(self.tasks(), function(task) { return !task.isDone() });
});
self.newTaskTextLength = ko.computed(function() {
if( this.newTaskText() ) {
var tasktext = this.newTaskText() + "";
return tasktext.length;
}
return 0;
}, self);
// Operations
self.addTask = function() {
$.parse.post('task', { title: this.newTaskText() }, function(task) {
self.tasks.push(new Task({ title: self.newTaskText(), objectId: task.objectId}));
self.newTaskText("");
});
};
self.removeTask = function(task) {
$.parse.delete('task/' + task.objectId, function() {
self.tasks.remove(task);
});
};
// Initialize data
$.parse.get("task", {}, function(json) {
for( var i = 0; i < json.results.length ; i++ ) {
var task = json.results[i];
// Create a new task
self.tasks.push(new Task({ title: task.title, objectId: task.objectId }));
}
});
}
ko.applyBindings(new TaskListViewModel());