Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created March 28, 2012 14:26
Show Gist options
  • Save mxriverlynn/2226605 to your computer and use it in GitHub Desktop.
Save mxriverlynn/2226605 to your computer and use it in GitHub Desktop.
composite views
// The recursive tree view
var TreeView = Backbone.Marionette.CompositeView.extend({
template: "#node-template",
tagName: "ul",
initialize: function(){
// grab the child collection from the parent model
// so that we can render the collection as children
// of this parent node
this.collection = this.model.nodes;
},
appendHtml: function(collectionView, itemView){
// ensure we nest the child list inside of
// the current list item
collectionView.$("li:first").append(itemView.el);
}
});
// The tree's root: a simple collection view that renders
// a recursive tree structure for each item in the collection
var TreeRoot = Backbone.Marionette.CollectionView.extend({
itemView: TreeView
});
// A Grid Row
var GridRow = Backbone.Marionette.ItemView.extend({
template: "#row-template",
tagName: "tr"
});
// The grid view
var GridView = Backbone.Marionette.CompositeView.extend({
tagName: "table",
template: "#grid-template",
itemView: GridRow,
appendHtml: function(collectionView, itemView){
collectionView.$("tbody").append(itemView.el);
}
});
@boxxxie
Copy link

boxxxie commented Jul 7, 2012

2.js simplification (don't know if it's best, if not please fix)

// A Grid Row
var GridRow = Backbone.Marionette.ItemView.extend({
    template: "#row-template",
    tagName: "tr"
});

// The grid view
var GridView = Backbone.Marionette.CompositeView.extend({
    tagName: "table",
    template: "#grid-template",
    itemView: GridRow,
    itemViewContainer: 'tbody'
});

@mxriverlynn
Copy link
Author

Good call @boxxxie - that option wasn't available when I wrote this, but it certainly does simply things now that it is available. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment