forked from jenimech/Testing-backbone-with-coffescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.js
47 lines (47 loc) · 1.3 KB
/
3.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
(function() {
(function($) {
var Item, List, ListView, listView;
Item = Backbone.Model.extend({
defaults: {
part1: "Hello",
part2: "Word"
}
});
List = Backbone.Collection.extend({
model: Item
});
ListView = Backbone.View.extend({
el: $('body'),
events: {
"click button#add": 'addItem'
},
initialize: function() {
_.bindAll(this, 'render', 'addItem', 'appendItem');
this.collection = new List();
this.collection.bind('add', this.appendItem);
this.counter = 0;
return this.render();
},
render: function() {
$(this.el).append("<button id='add'>Add list item</button>");
$(this.el).append("<ul></ul>");
return _(this.collection.models).each(function(item) {
return appendItem(item);
}, this);
},
addItem: function() {
var item;
this.counter++;
item = new Item();
item.set({
part2: item.get('part2') + this.counter
});
return this.collection.add(item);
},
appendItem: function(item) {
return $('ul', this.el).append("<li>" + item.get('part1') + " " + item.get('part2') + "</li>");
}
});
return listView = new ListView();
})($);
}).call(this);