/
incremental.html
97 lines (81 loc) · 2.6 KB
/
incremental.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Incremental List Example</title>
</head>
<body>
<!-- Layout will be inserted here -->
<div class="main"></div>
<!-- Layout template -->
<script type="template" id="main">
<h1>My super important list...</h1>
<p></p>
</script>
<!-- Item template -->
<script type="template" id="item">
<%= name %>
</script>
<!-- Required Library Dependencies -->
<script src="../test/vendor/jquery.js"></script>
<script src="../test/vendor/underscore.js"></script>
<script src="../test/vendor/backbone.js"></script>
<script src="../backbone.layoutmanager.js"></script>
<script>
// Create a Content view to be used with the Layout below.
var Item = Backbone.View.extend({
template: "#item",
tagName: "li",
serialize: function() {
return { name: this.model.get("name") };
}
});
var List = Backbone.View.extend({
// Since there is no template necessary for a List View we can simply
// set it's tagName to an <ol></ol>.
tagName: "ol",
// Custom render function is necessary to inject at runtime.
render: function(manage) {
// Iterate the collection, inserting a new Item View for each entry.
this.collection.each(function(model) {
// Using the insert without a selector will automatically insert
// the View into the root element... in this case its an <ol></ol>.
this.insertView(new Item({
model: model
}));
}, this);
// This is required for LayoutManager to handle View rendering.
return manage(this).render();
},
initialize: function() {
this.collection.on("add", function(model) {
this.insertView(new Item({ model: model })).render();
}, this);
}
});
// Create a new Layout with a sub view for content.
var main = new Backbone.LayoutManager({
template: "#main",
views: {
p: new List({
// Contrive a list of Models for the List Collection.
collection: new Backbone.Collection([
{ name: "Tom" },
{ name: "Mary" },
{ name: "Martin" }
])
})
}
});
window.setInterval(function() {
main.views.p.collection.add({ name: "Adam says put in the following: " +
+new Date() });
}, 1000);
// Attach the Layout to the main container.
main.$el.appendTo(".main");
// Render the Layout.
main.render();
</script>
</body>
</html>