Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save zeroeth/9908685 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/9908685 to your computer and use it in GitHub Desktop.
backbone collection
/* Sample table of data from collection */
/* Collection Partial */
var PartialView = Backbone.View.extend(
{
template: Mustache.compile ($("#PartialTemplate").html()),
tagName: "tr",
render: function()
{
var view = this.template(this.model.for_template());
this.$el.html(view);
return this;
},
});
/* Collection */
var CollectionView = Backbone.View.extend(
{
template: Mustache.compile ($("#CollectionTemplate").html()),
initialize: function()
{
var self = this; /* Scope in the closure */
this.listenTo(this.collection, "add", function (model) {
self.$('.Collection').append(new PartialView({model:model}).render().el);
;
});
},
render: function()
{
/* Container for list */
this.$el.html(this.template());
/* Reference class inside partial.. TODO find better way for this */
_.each(this.collection.models, function (model) {
this.$('.Collection').append(new PartialView({model:model}).render().el);
}, this);
return this;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment