Last active
December 26, 2015 19:09
-
-
Save rgwozdz/7199877 to your computer and use it in GitHub Desktop.
Backbone.js Collection View Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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