-
Notifications
You must be signed in to change notification settings - Fork 2
/
todos.js
142 lines (113 loc) · 3.34 KB
/
todos.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
jQuery(function($){
window.Todo = Backbone.Model.extend({
defaults: {
title: "Hello World",
done: false
},
toggle: function(){
this.save({done: !this.get("done")});
}
});
window.TodoCollection = Backbone.Collection.extend({
model: Todo,
localStorage: new Store("todos"),
done: function(){
return this.filter(function(todo){
return todo.get('done');
});
},
remaining: function(){
return this.filter(function(todo){
return !todo.get('done')
});
}
});
window.Todos = new TodoCollection;
window.TodoView = Backbone.View.extend({
tagName: "li",
events: {
"click li .checkbox": "toggleDone",
"click a.delete": "deleteTodo",
"click a.edit": "editTodo",
"dblclick span": "editTodo",
"click a.cancel": "cancelEdit",
"click .done": "doneEditing"
},
template: $("#todo-item").template(),
initialize: function(){
_.bindAll(this, "render", "toggleDone", "deleteTodo", "editTodo", "cancelEdit", "doneEditing");
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.remove, this);
},
toggleDone: function(){
this.model.toggle()
},
editTodo: function(){
$(".editing").removeClass("editing");
$(this.el).addClass('editing').focus();
},
cancelEdit: function(){
$(this.el).removeClass('editing');
},
doneEditing: function(){
this.model.save({content: this.$("input[type=text]").val()})
$(this.el).removeClass('editing');
},
deleteTodo: function(){
this.model.destroy()
},
render: function(){
var element = jQuery.tmpl(this.template, this.model.toJSON());
$(this.el).html(element);
return this;
}
});
window.AppView = Backbone.View.extend({
el: $("#app"),
events: {
"keypress #new-todo": "createTodo",
"click .clear-done": "clearDone",
"click .check-remaining": "checkRemaining"
},
initialize: function(){
_.bindAll(this, "addOne", "addAll", "clearDone", "checkRemaining", "render");
this.input = this.$("#new-todo");
Todos.bind('add', this.addOne); // when Todos.create is called, add is invoked
Todos.bind('reset', this.addAll); // refresh needs to fetch and add all to the page
Todos.fetch();
},
clearDone: function(){
_.each(Todos.done(), function(todo){
todo.destroy();
});
return false;
},
checkRemaining: function(){
var remaining = Todos.remaining()
if(remaining.length == 0){
_.each(Todos.done(), function(todo){
todo.toggle();
});
} else {
_.each(Todos.remaining(), function(todo){
todo.toggle();
});
}
},
addAll: function(){
Todos.each(this.addOne)
},
addOne: function(todo){
var view = new TodoView({model: todo});
this.$("#todo-list").prepend(view.render().el);
},
createTodo: function(e){
if(e.keyCode!=13) return;
var value = this.input.val();
if(!value) return;
Todos.create({content: value}); // will call add (which is bound above)
this.input.val(''); // reset the input field
}
});
window.App = new AppView;
});