/
feedView.js
57 lines (49 loc) · 1.91 KB
/
feedView.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
// # Feed View
//
// This is the view module for a Twitter feed.
//
module.declare(
[
{ backbone: 'vendor/backbone' }
, { feed: "js/model/feed" }
, { tweetView: "js/view/tweetView" }
]
, function (require, exports, module) {
var feed = require("feed").feed
, Backbone = require("backbone")
, TweetView = require("tweetView").TweetView
// The FeedView is a simple container of TweetViews and therefore
// doesn't need a template. The ul element provided by the Backbone
// View is sufficient.
, FeedView = Backbone.View.extend({
tagName: "ul"
, className: "feed"
, initialize: function() {
// Bind to model changes
feed.bind('add', this.addTweet, this);
feed.bind('reset', this.addAll, this);
// Load stored tweets from local storage
feed.fetch();
}
// Add a tweet to the view
// Creates a new TweetView for the tweet model
// and adds it to the FeedView
, addTweet: function(tweet) {
var tweetView = new TweetView({
model: tweet
});
var el = tweetView.render().el;
$(this.el).append(el);
return this;
}
// Handle resets to the model by adding all new elements to the view
// Existing tweet views will have been removed when their models are
// destroyed.
, addAll: function() {
var that = this;
feed.each(function(tweet){that.addTweet(tweet)});
}
});
exports.FeedView = FeedView;
}
);