-
-
Save mxriverlynn/2226605 to your computer and use it in GitHub Desktop.
composite views
This file contains 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
// 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 | |
}); |
This file contains 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
// 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); | |
} | |
}); |
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
2.js simplification (don't know if it's best, if not please fix)