【问题标题】:Pass compositeView option to itemView将复合视图选项传递给 itemView
【发布时间】:2014-05-22 21:28:25
【问题描述】:

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

复合视图:

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 中?

【问题讨论】:

    标签: javascript backbone.js marionette


    【解决方案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 指定为函数 计算要在运行时返回的值。模型将通过 计算时是否需要访问该函数 项目视图选项。该函数必须返回一个对象,并且 对象的属性将被复制到 itemView 实例的 选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2023-04-08
      • 1970-01-01
      • 2017-06-06
      • 2012-12-17
      相关资源
      最近更新 更多