Skip to content

Instantly share code, notes, and snippets.

@rgwozdz
Last active December 26, 2015 19:09
Show Gist options
  • Save rgwozdz/7199877 to your computer and use it in GitHub Desktop.
Save rgwozdz/7199877 to your computer and use it in GitHub Desktop.
Backbone.js Collection View Template
Backbone = (function (module) {
module.CollectionView = Backbone.View.extend({
initialize: function (args) {
var self, viewClass;
var self = this;
viewClass = (typeof args.modelView === 'undefined') ? Backbone.View : args.viewClass;
// Create an array property that will store model views contained in this collection view
this.componentViews = [];
// Loop thru the collection
this.collection.forEach( function(colModel, index) {
// Create a view for each model contained in this view's referenced collection - each view is a WMS list-item
var modelView = new viewClass({model:colModel});
// Store this in the collection view's componentView array
self.componentViews.push(modelView);
});
},
// This view's wrapper tag is a ul
tagName: 'ul',
// This view's wrapper css class
className: '',
// rendering function for this view
render: function(){
var self = this;
// Render and append each model view of collection (table rows)
_.each(this.componentViews, function(view){
// Render the component view
view.render();
// Append the view to this collection view element
self.$el.append(view.el);
});
}
});
return module;
}( Backbone));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment