App.Views.Stream = Backbone.View.extend({ events: { "click #paginate": "loadMore" }, initialize: function() { _.bindAll(this, "appendPost", "collectionFetched", "loadMore"); this.collection = this.collection || new App.Collections.Stream; this.collection.bind("add", this.appendPost); }, render : function(){ _.each(this.collection.models, this.appendPost) return this; }, appendPost: function(post) { var postView = new App.Views.Post({ model: post }).render(); $(this.el).append(postView.el); }, collectionFetched: function() { this.$("#paginate").remove(); $(this.el).append($("", { href: this.collection.url(), id: "paginate" }).text('Load more posts')); }, loadMore: function(evt) { if(evt) { evt.preventDefault(); } this.addLoader(); this.collection.fetch({ add: true, success: this.collectionFetched }); }, addLoader: function(){ this.$("#paginate").html($("", { src : "/images/static-loader.png", "class" : 'loader' })); } });