Skip to content

Instantly share code, notes, and snippets.

@gsmaverick
Created April 5, 2012 16: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 gsmaverick/2312413 to your computer and use it in GitHub Desktop.
Save gsmaverick/2312413 to your computer and use it in GitHub Desktop.
Handling tree-like layouts in Backbone
var TreeItemView = Backbone.View.extend({
tagName: 'div',
className: 'tree-item',
render: function() {
var self = this;
// Some function that retrieves an array of children for this model
var children = TreeCollection.children(this.model.get('id'));
// Render this tree item
$(this.el).html(this.template({ this.model.toJSON(); }));
// Render all the children of this tree item
_.each(children, function(child) {
var view = new TreeItemView({ model: child });
$(self.el).append(view.render().el);
});
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment