Created
September 10, 2012 19:51
-
-
Save ciberch/3693398 to your computer and use it in GitHub Desktop.
The Activity Stream View in Backbone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ActivityStreamView = Backbone.View.extend({ | |
el: '#main_stream', // el attaches to existing element | |
initialize: function(){ | |
_.bindAll(this, 'render', 'appendItem'); // every function that uses 'this' as the current object should be in here | |
this.collection = new ActivityList(); | |
this.collection.bind('add', this.appendItem); // collection event binder | |
this.maxSize = 20; | |
}, | |
render: function(){ | |
_(this.collection.models).each(function(item){ // in case collection is not empty | |
self.appendItem(item); | |
}, this); | |
}, | |
appendItem: function(item){ | |
var itemView = new ActivityView({ model: item }); | |
this.$el.prepend(itemView.render().el); | |
if (this.el.children.count > this.maxSize) { | |
this.el.children.last.remove(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment