Skip to content

Instantly share code, notes, and snippets.

@kn0ll
Created June 26, 2013 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn0ll/5868489 to your computer and use it in GitHub Desktop.
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
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