我有一个包含 itemView 的复合 View 。我在实例化时通过选项对象将一个值传递到复合 View 中。在compositeView 中,我将itemview 属性设置为itemview,并使用itemViewOptions 属性尝试将选项中的值传递到复合 View 中。这是我的代码:
复合 View :
myFirstCompositeView = Marionette.CompositeView.extend({
template: Handlebars.templates["myTemp"],
initialize: function(options){
//this console statement works as expected options are there
console.log("myFirstCompositeView.initialize() -> options -> ", options);
this.eventBus = options.eventBus;
this.mapModel = options.myModel;
//i tried this
this.itemView : myFirstItemView;
this.itemViewOptions = this.myModel;
},
i also tried this...
itemView : myFirstItemView
itemViewOptions = this.myModel;
});
项目 View :
myFirstItemView = SegmentItemView = Marionette.ItemView.extend({
template: Handlebars.templates["myothertemp"],
initialize : function(options){
//value undefined
console.log("myFirstItemView .initialize() -> ", options.myModel);
},
});
CompositeView 的实例化:
new myFirstCompositeView ({
myModel : {testval : 777, teststr: "holy cow"},
collection: model.get("myFirstCollection"),
eventBus: eventBus
}));
是否有办法将值传递到 itemView 中?
最佳答案
试试这个:
myFirstCompositeView = Marionette.CompositeView.extend({
template: Handlebars.templates["myTemp"],
initialize: function(options){
this.eventBus = options.eventBus;
this.mapModel = options.myModel;
},
itemView : myFirstItemView,
itemViewOptions: function(){
return {
myModel: this.myModel
};
}
});
From the Marionette documentation:
You can also specify the itemViewOptions as a function, if you need to calculate the values to return at runtime. The model will be passed into the function should you need access to it when calculating itemViewOptions. The function must return an object, and the attributes of the object will be copied to the itemView instance's options.
关于javascript - 将compositeView选项传递给itemView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23817360/