forked from parasj/backbone-rssreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
166 lines (143 loc) · 4.47 KB
/
code.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
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
$(function() {
var Blog = Backbone.Model.extend({
defaults: function() {
return {
title: "Paras Jain's Blog",
desc: "Paras Jain's Blog",
url: "http://blog.parasjain.com/"
};
},
initialize: function() {
if(!this.get("title")) {
this.set({
"title": this.defaults.title
});
}
if(!this.get("desc")) {
this.set({
"desc": this.defaults.desc
});
}
}
});
var Article = Backbone.Model.extend({
defaults: function() {
return {
title: "Empty Title",
desc: "Empty Description",
url: "http://blog.parasjain.com/"
};
},
initialize: function() {
if(!this.get("title")) {
this.set({
"title": this.defaults.title
});
}
if(!this.get("desc")) {
this.set({
"desc": this.defaults.desc
});
}
},
read: function() {
this.save({
read: true
});
},
remove: function() {
this.destroy();
}
});
// Collection: ArticleList
var ArticleList = Backbone.Collection.extend({
model: Article,
localStorage: new Store("reader"),
read: function() {
return this.filter(function(article) {
return article.get('read');
});
},
unread: function() {
return this.without.apply(this, this.read());
},
clear: function() {
return this.each(function(article) {
article.remove();
});
}
});
var Articles = new ArticleList;
// View: ArticleView
var ArticleView = Backbone.View.extend({
tagName: "tr",
template: _.template($('#item-template').html()),
events: {
"click .article-item": "triggerNav"
},
initialize: function() {
this.model.bind('change', this.render, this);
this.model.bind('destroy', this.remove, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
triggerNav: function() {
var url = this.model.toJSON().url;
console.log("Nav change requested:", url);
window.location = url;
}
});
// View: AppView
var AppView = Backbone.View.extend({
el: $("#readerapp"),
events: {
"click button#newArticle": "createNewArticle",
"click button#clearArticles": "clearArticles",
},
initialize: function() {
jQuery.getFeed({
url: 'rss.xml',
success: function(feed) {
$("#blogtitle").html(feed.title);
_.each(feed.items, function(blogItem) {
console.log(blogItem);
var description = $(blogItem.description).text().substr(0, 300);
description = description.substr(0, Math.min(description.length, description.lastIndexOf(" "))) + " . . .";
Articles.create({
title: blogItem.title,
desc: description,
url: blogItem.link,
date: blogItem.updated
});
});
}
});
Articles.bind('add', this.addOne, this);
Articles.bind('reset', this.addAll, this);
// Articles.fetch();
},
addOne: function(article) {
var view = new ArticleView({
model: article
});
this.$("#article-list").append(view.render().el);
},
addAll: function() {
Articles.each(this.addOne);
},
createNewArticle: function() {
console.log(0)
var article = Articles.create({
title: "New Article",
desc: "This must really be an interesting article..."
});
},
clearArticles: function() {
Articles.clear();
this.addAll();
}
});
var App = new AppView;
});