我做的極其相似,在這篇文章中所描述的樹狀結構的東西:選項傳遞從的CollectionView遞歸CompositeView中
http://lostechies.com/derickbailey/2012/04/05/composite-views-tree-structures-tables-and-more/
的想法是有集合視圖中根節點的集合( TreeRoot),其中每個根節點也有一個遞歸合成視圖(TreeView)。一的jsfiddle顯示簡單的工作示例從aricle樹結構可以在這裏找到:
http://jsfiddle.net/hoffmanc/NH9J6/
我想修改的例子使一些選項可以傳遞給每個樹視圖,無論它在樹結構中。這是我的(只有非常輕微的修改)的jsfiddle:
在我的變形例的JS代碼:
// The recursive tree view
var TreeView = Backbone.Marionette.CompositeView.extend({
template: "#node-template",
tagName: "li",
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;
console.log(this.options.msg); // undefined!
},
appendHtml: function(cv, iv){
cv.$("ul:first").append(iv.el);
},
onRender: function() {
if(_.isUndefined(this.collection)){
this.$("ul:first").remove();
}
}
});
// 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({
tagName: "ul",
itemView: TreeView
});
// ----------------------------------------------------------------
// Below this line is normal stuff... models, templates, data, etc.
// ----------------------------------------------------------------
treeData = [
{
nodeName: "top level 1",
nodes: [
{
nodeName: "2nd level, item 1",
nodes: [
{ nodeName: "3rd level, item 1" },
{ nodeName: "3rd level, item 2" },
{ nodeName: "3rd level, item 3" }
]
},
{
nodeName: "2nd level, item 2",
nodes: [
{ nodeName: "3rd level, item 4" },
{
nodeName: "3rd level, item 5",
nodes: [
{ nodeName: "4th level, item 1" },
{ nodeName: "4th level, item 2" },
{ nodeName: "4th level, item 3" }
]
},
{ nodeName: "3rd level, item 6" }
]
}
]
},
{
nodeName: "top level 2",
nodes: [
{
nodeName: "2nd level, item 3",
nodes: [
{ nodeName: "3rd level, item 7" },
{ nodeName: "3rd level, item 8" },
{ nodeName: "3rd level, item 9" }
]
},
{
nodeName: "2nd level, item 4",
nodes: [
{ nodeName: "3rd level, item 10" },
{ nodeName: "3rd level, item 11" },
{ nodeName: "3rd level, item 12" }
]
}
]
}
];
TreeNode = Backbone.Model.extend({
initialize: function(){
var nodes = this.get("nodes");
if (nodes){
this.nodes = new TreeNodeCollection(nodes);
this.unset("nodes");
}
}
});
TreeNodeCollection = Backbone.Collection.extend({
model: TreeNode
});
var tree = new TreeNodeCollection(treeData);
var treeView = new TreeRoot({
collection: tree,
itemViewOptions: {msg: 'hi'}
});
正如你所看到的,我試圖用itemViewOptions選項傳遞,但是當TreeView初始化時,它不在那裏。我假設這是因爲只有TreeRoot獲得itemViewOptions。
我的問題是:如何將選項從TreeRoot傳遞給TreeView?
謝謝!
見我回答這個問題。 http://stackoverflow.com/a/16427755/693799 – 2013-05-08 17:00:46
傳遞選項只是簡單的使用Backbone,但我不知道如何做到這一點,特別是在這種情況下,有一個遞歸的複合視圖... – stank345 2013-05-08 17:13:33