Last active
August 29, 2015 13:57
-
-
Save zeroeth/9908685 to your computer and use it in GitHub Desktop.
backbone 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
| /* 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