Skip to content

Instantly share code, notes, and snippets.

@ciberch
Created September 10, 2012 19:51
Show Gist options
  • Save ciberch/3693398 to your computer and use it in GitHub Desktop.
Save ciberch/3693398 to your computer and use it in GitHub Desktop.
The Activity Stream View in Backbone
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