1 /** 2 * Comment list controller and view 3 * Subscribes to comment collection events and renders a list of comments according 4 * 5 * @class CommentlistView 6 * @extends Backbone.View 7 * @author Bodnar Istvan <istvan@gawker.com> 8 */ 9 /*global CommentView */ 10 var CommentlistView = Backbone.View.extend( 11 /** @lends CommentlistView.prototype */ 12 { 13 /** 14 * Subscribe to collection 'reset' and 'add' events 15 */ 16 initialize: function () { 17 this.collection.on('add reset', this.render, this); 18 }, 19 20 /** 21 * Render comments using CommentView instances for each model in the collection. 22 * @returns {CommentlistView} Returns the view instance itself, to allow chaining view commands. 23 */ 24 render: function () { 25 // first clean up the container 26 this.$el.empty(); 27 28 // iterate over models in collection and render comments using the CommentView view class 29 this.collection.each(function (item) { 30 // create new CommentView instance 31 var commentview = new CommentView({ 32 model: item 33 }); 34 35 // append rendered CommentView instance to CommentlistViews container 36 this.$el.append(commentview.render().$el); 37 }, this); 38 39 return this; 40 } 41 } 42 );