Created
June 26, 2013 15:39
-
-
Save kn0ll/5868489 to your computer and use it in GitHub Desktop.
simple backbone collection layout. extends backbone.layout.js. accepts an `ItemView` and a `collection` and appends a new `ItemView` to it's parent container for each model in the collection
This file contains hidden or 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.CollectionLayout = Backbone.Layout.extend({ | |
initialize: function() { | |
Backbone.Layout.prototype.initialize.apply(this, arguments); | |
this.listenTo(this.collection, 'add', _.bind(this.modelAdded, this)); | |
}, | |
ItemView: Backbone.Layout, | |
modelAdded: function(model) { | |
var $el = this.$el, | |
view = new this.ItemView({ model: model }); | |
this.viewReferences[model] = view; | |
$el.append(view.render().el); | |
return view; | |
}, | |
// renders the layout and its subviews | |
render: function() { | |
var self = this, | |
views = self.views? self.views(): [], | |
$el = this.$el, | |
collection = this.collection, | |
ItemView = this.ItemView; | |
// render current | |
$el.empty(); | |
// render subviews | |
collection.each(_.bind(this.modelAdded, this)); | |
return this; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment