-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (77 loc) · 2.22 KB
/
app.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
(function() {
(function($) {
var Item, ItemView, Items, ViewList, viewList;
Item = Backbone.Model.extend({
defaults: {
name: "<b style='color:red;'>Empty name</b>"
}
});
Items = Backbone.Collection.extend({
model: Item
});
ItemView = Backbone.View.extend({
events: {
"click a.remove": 'unrender'
},
initialize: function() {
_.bindAll(this, 'render', 'unrender', 'remove');
return this.model.bind('remove', 'unrender');
},
render: function() {
$(this.el).html("<li><span>" + this.model.get('name') + " </span><a href='#' class='remove'>[delete]</a></li>");
return this;
},
unrender: function() {
return $(this.el).remove();
},
remove: function() {
return this.model.destroy();
}
});
ViewList = Backbone.View.extend({
el: $('body'),
events: {
"keypress #new-todo": "addItem"
},
initialize: function() {
_.bindAll(this, 'render', 'addItem', 'appendItem');
this.collection = new Items();
this.collection.bind('add', this.appendItem);
return this.render();
},
render: function() {
$(this.el).append("<h2>ToDo</h2>");
$(this.el).append("<input id='new-todo' placeholder='What needs to be done?'' type='text' />");
$(this.el).append("<ul></ul>");
return _(this.collection.models).each(function(item) {
return appendItem(item);
}, this);
},
addItem: function(e) {
var item, task_name;
if (e.keyCode !== 13) {
return;
}
item = new Item();
task_name = $('input#new-todo')[0].value;
if (task_name === '') {
return alert("you can't add an empty item.");
} else {
item.set({
name: task_name
});
$('input#new-todo')[0].value = '';
return this.collection.add(item);
}
},
appendItem: function(item) {
var itemView;
itemView = new ItemView({
model: item
});
return $('ul', this.el).append(itemView.render().el);
}
});
return viewList = new ViewList();
})($);
}).call(this);