javascript - 将compositeView选项传递给itemView

标签 javascript backbone.js marionette

我有一个包含 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/

相关文章:

javascript - 在同一页面上运行多个 ng-app 容器

backbone.js - 当我更改某些属性的值或从列表中删除时如何通知 View ?

backbone.js - 主干模型保存,验证失败

javascript - 将模型添加到集合中

javascript - 无法在 Marionette 布局中使用 UI 哈希找到元素

javascript - Backbone.Marionette - 以某种方式收集 "missing"一些元素

javascript - 如何使一条线平滑而另一条线不在图表上?

javascript - jQuery 如何使 div 名称始终为 1,2,3

javascript - 使用 graphql 在 react-native 中重新获取查询

javascript - Marionette JS : updating all ItemViews in a CompositeView after a 'change' event in the DOM