我有一个具有itemView的CompositeView。我正在通过options对象将值传递到实例化的CompositeView中。在compositeView中,我将itemview属性设置为itemview,并且我正在使用itemViewOptions属性来尝试传递传入组合视图中的选项的值。这是我的代码:

CompositeView:

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;
});


项目视图:

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:


  如果需要,还可以将itemViewOptions指定为函数。
  计算要在运行时返回的值。模型将通过
  计算中是否需要访问该函数
  itemViewOptions。该函数必须返回一个对象,并且
  对象的属性将复制到itemView实例的
  选项。

10-06 04:02