public
Created

The Activity Stream View in Backbone

  • Download Gist
activity_stream_view.js
JavaScript
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
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();
}
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.