-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
95 lines (72 loc) · 2.04 KB
/
models.js
File metadata and controls
95 lines (72 loc) · 2.04 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var Task = Backbone.Model.extend({
defaults: {
'text': '',
'in_lists': [],
'done': false
},
initialize: function(){
console.info('Task.init', this.toJSON());
return this;
},
toggle_state: function() {
this.save({
done: !this.get('done')
});
return this;
}
});
var List = Backbone.Model.extend({
defaults: {
'name': 'Untitled',
'start_date': null,
'end_date': null
},
initialize: function(){
console.info('List.init =>', this.get('name'));
return this;
},
get_tasks: function(){
var list_id = this.id;
return Tasks.filter(function(task){
if( _.indexOf( task.get('in_lists'), list_id ) >= 0 ) {
return task;
}
}, this);
}
});
var Tasks = Backbone.Collection.extend({
model: Task,
url: '/tasks',
localStorage: new Store("tasks"),
initialize: function(){
console.info('Tasks.init', this.model);
this.bind('change', function(task) {
task.TaskView && task.TaskView.render();
console.warn('Save', task)
}).bind('add', function(task) {
TasksView.add_task(task);
task.save();
});
return this;
}
});
var Lists = Backbone.Collection.extend({
model: List,
localStorage: new Store("lists"),
url: '/lists',
initialize: function(){
console.info('Lists.init', this.model);
this.bind('change', function(list, options) {
list.ListView && list.ListView.render();
TasksView.rename(list);
}).bind('add', function(list) {
ListsView.add_list(list);
list.save();
}).bind('remove', function(list) {
_.each(list.get_tasks(), function(task) {
task.destroy();
});
});
return this;
}
});