-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.html
169 lines (141 loc) · 4.09 KB
/
demo.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<html>
<head>
<title></title>
<link rel='stylesheet' href='demo.css' type='text/css'>
<script type='text/javascript' src='lib/jquery.js'></script>
<script type='text/javascript' src='lib/underscore.js'></script>
<script type='text/javascript' src='lib/backbone.js'></script>
<script type="text/javascript" src="src/backbone_query_parser.js"></script>
<script type="text/javascript" src="src/backbone_routes.js"></script>
<script type="text/javascript" src="src/backbone_memory_store.js"></script>
<script type="text/javascript" src="lib/mustache.js"></script>
<script type='text/javascript'>
var App = {
Controllers: {},
Models: {},
Collections: {},
Views: {}
};
var Post = Backbone.Model.extend({
url: function() {
return App.Collections.Posts.prototype.url;
},
initialize: function() {
for(var attr in this.attributes) {
this[attr] = this.attributes[attr];
}
},
parse: function(response) {
var post, self = this;
_.each(response.models, function(model, i) {
if(new Post(model.attributes).permalink() == self.permalink)
post = model;
});
return post.attributes;
},
fetch: function(options) {
var self = this;
var collection = new App.Collections.Posts();
collection.fetch({
success: function(response) {
self.attributes = self.parse(response);
self.initialize();
options.success();
}
});
},
permalink: function() {
return this.get('title').replace(/[^\w]/gi, '-')
}
});
App.Collections.Posts = Backbone.Collection.extend({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D'http%3A%2F%2Fkeithnorm.com%2Ffeed%2F'%3B&format=json&diagnostics=true&callback=?",
model: Post,
parse: function(response) {
return response.query.results.item;
}
});
App.Controllers.Demo = Backbone.Controller.extend({
routes: {
'' : {to: 'index', as: 'posts'},
'/posts' : {to: 'index', as: 'posts'},
'/posts/:permalink': {to: 'show', as: 'post'}
},
initialize: function() {
},
index: function() {
console.log('rendered posts path', this.postsPath());
var posts = new App.Collections.Posts();
posts.fetch({
success: function() {
new App.Views.Posts({
collection: posts
});
}
});
},
show: function(params) {
var post = new Post({permalink: params.permalink});
post.fetch({
success: function() {
new App.Views.Post({model: post});
}
});
}
});
App.Views.Posts = Backbone.View.extend({
el: '#posts',
events: {},
template: '{{# posts }}<div class="example">\
<h2><a href="#{{ href }}">{{ title }}</a></h2>\
</div>\
{{/ posts }}',
initialize: function() {
console.log('init view', this);
this.render();
},
render: function() {
var models = _.map(this.collection.models, _.bind(function(model, i) {
return this._wrapViewMethods(model);
}, this));
$(this.el).html(Mustache.to_html(this.template, {posts: models}));
},
_wrapViewMethods: function(model) {
model.href = this.postPath(model.permalink());
return model;
}
});
App.Views.Post = Backbone.View.extend({
el: '#posts',
events: {},
template: '<div class="example">\
<h2><a href="#{{ href }}">{{ title }}</a></h2>\
{{{ encoded }}}\
</div>',
initialize: function() {
this.render();
},
render: function() {
$(this.el).html(Mustache.to_html(this.template, this._wrapViewMethods(this.model)));
},
_wrapViewMethods: function(model) {
model.href = this.postPath(model.permalink);
return model;
}
});
$(function() {
new App.Controllers.Demo();
Backbone.history.start();
});
</script>
</head>
<body>
<div id='container'>
<h1>
<a href='#posts'>Backbone Routes Demo</a>
</h1>
<div id='posts'>
</div>
</div>
</body>
</html>