-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.js
More file actions
78 lines (61 loc) · 1.71 KB
/
4.js
File metadata and controls
78 lines (61 loc) · 1.71 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
(function($) {
var Item = Backbone.Model.extend({
defaults: {
part1: 'hello',
part2: 'world'
}
});
var List = Backbone.Collection.extend({
model: Item
});
var ItemView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
alert( "Hello 2a" );
_.bindAll( this, 'render' );
},
render: function() {
alert( "Hello 2b" );
$(this.el).html('<span>' + this.model.get('part1') + ' ' + this.model.get('part2') + '</span>');
alert( "Hello 2c" + '<span>' + this.model.get('part1') + ' ' + this.model.get('part2') + '</span>' );
return this;
}
});
var ListView = Backbone.View.extend({
el: $('body'),
events: {
//alert( "Hello event" );
'click button#add': 'addItem'
},
initialize: function() {
_.bindAll(this, 'render', 'addItem', 'appendItem' );
this.collection = new List( {part1: "New", part2: "Bean"} );
this.collection.bind('add', this.appendItem);
this.counter = 0;
this.render();
},
render: function() {
var self = this;
$(this.el).append("<button id='add'>Add list item</button>");
$(this.el).append("<ul></ul>");
alert( "Hello ?" );
_(this.collection.models).each(function( item ) {self.appendItem( item ); }, this);
alert( "Hello ?a" );
},
addItem: function() {
this.counter++;
alert( "Hello counter " + this.counter );
var item = new Item();
item.set({part2: item.get('part2') + this.counter});
this.collection.add( item );
},
appendItem: function(item) {
alert("Hello 1");
var itemView = new ItemView({model: item});
alert("Hello 2");
$('ul', this.el).append(itemView.render().el);
alert("Hello 3");
}
});
var listView = new ListView();
}) (jQuery)