将compositeView选项传递给itemView

时间:2014-05-22 21:28:26

标签: javascript backbone.js marionette

我有一个包含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;
});

ItemView控件:

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?

1 个答案:

答案 0 :(得分:1)

试试这个:

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实例中   选项。