Backbone.js 木偶网:将数据从CollectionView传递到ItemView,再传递到ItemView

Backbone.js 木偶网:将数据从CollectionView传递到ItemView,再传递到ItemView,backbone.js,marionette,Backbone.js,Marionette,使用下面的结构,我成功地将数据从CollectionView传递到ItemView。但是,当我尝试将相同的数据传递给sub-ItemView(并呈现数据)时,数据对象是未定义的 有什么想法吗?下面是我尝试过的一种方法的代码 var myView = Backbone.Marionette.CollectionView.extend({ itemView: myItemView, initialize: function(options) { this.model

使用下面的结构,我成功地将数据从CollectionView传递到ItemView。但是,当我尝试将相同的数据传递给sub-ItemView(并呈现数据)时,数据对象是未定义的

有什么想法吗?下面是我尝试过的一种方法的代码

var myView = Backbone.Marionette.CollectionView.extend({
    itemView: myItemView,

    initialize: function(options) {
        this.model = options.model;
    },
    itemViewOptions: function(model,index){
        return{
            viewModel: this.model
        }
    }

});

var myItemView = Backbone.Marionette.ItemView.extend({
    template: _.template(Template),

    initialize: function(options) {
        this.viewModel = options.viewModel;
    },

    onRender: function(options) {
            // I thought the line below was a method for passing options from one ItemView into another.
        var view = new mySecondItemView ({model: this.viewModel});
        view.render();

        var temp = this.$el["0"]['innerHTML'];
            // The line below proves to my satisfaction that the ItemView is receiving the data model from the CollectionView
        temp += this.viewModel.get("property_name");
        this.$el["0"]['innerHTML'] = temp;
    }
});


var mySecondItemView = Backbone.Marionette.ItemView.extend({

        // And I thought I could set the viewModel in this function by assigning it the value of the model that was passed through the options.
    initialize: function(options) {
        this.viewModel = options.model;
    },

        // Finally I expect to be able to display information from the data model in the following way. The line below is what tips me off the the fact that viewModel in this second, sub-ItemView is undefined.
    template: _.template('<p><%= this.viewModel.get("property_name") %></p>')
});
var myView=Backbone.marionete.CollectionView.extend({
itemView:myItemView,
初始化:函数(选项){
this.model=options.model;
},
itemViewOptions:函数(模型、索引){
返回{
viewModel:this.model
}
}
});
var myItemView=Backbone.marionete.ItemView.extend({
模板:\模板(模板),
初始化:函数(选项){
this.viewModel=options.viewModel;
},
onRender:函数(选项){
//我认为下面这行是一种将选项从一个ItemView传递到另一个ItemView的方法。
var view=newmysecondetimview({model:this.viewModel});
view.render();
var temp=this.$el[“0”]['innerHTML'];
//下面的一行令人满意地证明ItemView正在从CollectionView接收数据模型
temp+=this.viewModel.get(“属性名称”);
此.$el[“0”]['innerHTML']=temp;
}
});
var mysecondemview=Backbone.marionete.ItemView.extend({
//我想我可以在这个函数中设置viewModel,方法是给它分配通过选项传递的模型的值。
初始化:函数(选项){
this.viewModel=options.model;
},
//最后,我希望能够以以下方式显示数据模型中的信息。
模板:u.template(“

”) });
使用下面的代码,我可以通过仅声明属性名称来访问子项视图中模型的属性,因此我可以编写
,而不是
this.model.get(“property_name”)

var myView=Backbone.marionete.CollectionView.extend({/*此处无任何更改*/});
var myItemView=Backbone.marionete.ItemView.extend({
模板:\模板(模板),
初始化:函数(选项){
this.viewModel=options.viewModel;
},
onRender:函数(选项){
//这确实会将数据传递到sub-ItemView
var view=newmysecondetimview({model:this.viewModel});
view.render();
var temp=this.$el[“0”]['innerHTML'];
//下面的行提取在sub-ItemView中呈现的HTML
temp+=''+视图。$el['0']['innerHTML']+'';
此.$el[“0”]['innerHTML']=temp;
}
});
var mysecondemview=Backbone.marionete.ItemView.extend({
//像这样显示属性。。。
模板:u.template(“

”) });
为什么CollectionView没有收藏,只有模型?
var myView = Backbone.Marionette.CollectionView.extend({ /* Nothing changes here */ });

var myItemView = Backbone.Marionette.ItemView.extend({
    template: _.template(Template),

    initialize: function(options) {
        this.viewModel = options.viewModel;
    },

    onRender: function(options) {
            // This does indeed pass the data to the sub-ItemView
        var view = new mySecondItemView ({model: this.viewModel});
        view.render();

        var temp = this.$el["0"]['innerHTML'];
            // The line below pulls the HTML rendered in the sub-ItemView
        temp += '<div class="content">'+view.$el['0']['innerHTML']+'</div>';
        this.$el["0"]['innerHTML'] = temp;
    }
});


var mySecondItemView = Backbone.Marionette.ItemView.extend({

        // Display a property like so...
    template: _.template('<p><%= property_name %></p>')
});