Last active
December 17, 2015 00:38
-
-
Save jogjayr/5521954 to your computer and use it in GitHub Desktop.
A simple View collection for BackboneJS views. Built for those use cases where you want a set of child views inside a parent view. Classic example: <ul id='item_list'> <li class='list_item'>An item</li> <li class='list_item'>Another item</li>
</ul>
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.ViewCollection = Backbone.View.extend({ | |
delegateChildEvents: function() { | |
_.each(this.childViews, function (childView) { | |
childView.delegateEvents(); | |
}); | |
}, | |
render: function() { | |
if(!this.collection) { | |
throw "ViewCollection needs a collection of models to render"; | |
} | |
if(!this.options.BaseViewClass) { | |
throw "Need a base view class"; | |
} | |
var me = this, | |
BaseView = this.options.BaseViewClass; | |
me.$el.empty(); | |
me.collection.each(function (model) { | |
var new_item_view = new BaseView({ | |
model: model | |
}).render(); | |
me.childViews.push(new_item_view); | |
me.$el.append(new_item_view.$el); | |
}); | |
me.delegateEvents(); | |
return this; | |
}, | |
childViews: [], | |
initialize: function (config) { | |
}, | |
delegateEvents: function () { | |
Backbone.View.prototype.delegateEvents.call(this); | |
this.delegateChildEvents(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment