/
07-basic-router.html
119 lines (90 loc) · 2.42 KB
/
07-basic-router.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
<head>
<title>Basic Router</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/json2-min.js"></script>
<script src="js/underscore-1.4.4-min.js"></script>
<script src="js/backbone-1.0.0-min.js"></script>
<script>
var app = (function($){
var Wine = Backbone.Model.extend({
schemaName : "wine"
});
var Wines = Backbone.Collection.extend({
model: Wine
});
var wines = new Wines();
var HomeView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
this.$el.empty();
this.$el.append("<h1>Wine Cellar</h1>");
this.$el.append("<a href='#add'>Add View</a>");
this.listView = new ListView();
this.$el.append(this.listView.render().el);
return this;
}
});
var ListView = Backbone.View.extend({
tagName: 'ul',
initialize: function() {
this.template = _.template($('#listTemplate').html());
this.listenTo(wines, "all", this.render);
this.render();
},
render: function() {
var self = this;
this.$el.empty();
wines.each(function(model) {
self.$el.append(self.template(model.toJSON()));
});
return this;
}
});
var AddView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
this.$el.append("Add View Here!")
return this;
}
});
var AppRouter = Backbone.Router.extend({
routes:{
"":"home",
"add":"add"
},
home:function () {
var view = new HomeView();
$('body').empty();
$('body').append(view.el);
},
add:function () {
var view = new AddView();
$('body').empty();
$('body').append(view.el);
}
});
var initialize = function() {
wineApp = new AppRouter();
Backbone.history.start();
}
return { initialize : initialize};
}(jQuery));
$(document).ready(function() {
app.initialize();
});
</script>
<script type="text/template" id="listTemplate">
<li>
<%= winery %>
</li>
</script>
</head>
<body style="padding:20px"></body>
</html>