forked from jenimech/Testing-backbone-with-coffescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.js
84 lines (84 loc) · 2.54 KB
/
5.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
(function() {
(function($) {
var Item, ItemView, List, ListView, listView;
Backbone.sync = function(method, model, success, error) {
return success();
};
Item = Backbone.Model.extend({
defaults: {
part1: "Hello",
part2: "world"
}
});
List = Backbone.Collection.extend({
model: Item
});
ItemView = Backbone.View.extend({
tagName: 'li',
events: {
"click span.swap": 'swap',
"click span.delete": 'remove'
},
initialize: function() {
_.bindAll(this, 'render', 'unrender', 'swap', 'remove');
this.model.bind('change', this.render);
return this.model.bind('remove', this.unrender);
},
render: function() {
$(this.el).html('<span style="color:black;">' + this.model.get('part1') + ' ' + this.model.get('part2') + '</span> <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
return this;
},
unrender: function() {
return $(this.el).remove();
},
swap: function() {
var swapped;
swapped = {
part1: this.model.get('part2'),
part2: this.model.get('part1')
};
return this.model.set(swapped);
},
remove: function() {
return this.model.destroy();
}
});
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) {
var itemView;
itemView = new ItemView({
model: item
});
return $('ul', this.el).append(itemView.render().el);
}
});
return listView = new ListView();
})($);
}).call(this);