我有Marionette / Backbone appliaction,工作正常。我想在视图中添加额外的图层:
之前:
TabLayoutView -> CompositeView
之后:
TabLayoutView -> SectionLayoutView -> CompositeView
但这不起作用,我看不出问题出在哪里。
以下是代码:
标签模型:
TabModel = Backbone.Model.extend({
defaults: {
headerModel: {label: '', left: '', right: ''}
}
})
标签模板:
<div class="headerSection"></div>
标签视图:
var TabLayoutView = Marionette.LayoutView.extend({
template: _.template(TabTemplate),
tagName: 'div',
regions: {
headerRegion: {selector: '.headerSection'}
},
onShow: function() {
this.headerRegion.show(new SectionLayoutView({model: this.model.get('headerModel')}));
}
});
部分模型:
SectionModel = Backbone.Model.extend({
defaults: {
label: '',
left: '',
right: ''
}
});
部分模板:
<div class="section">
<div class="leftSection"/>
<div class="rightSection"/>
</div>
部分视图:
SectionLayoutView = Marionette.LayoutView.extend({
template: _.template(SectionTemplate),
tagName: 'div',
regions: {
leftRegion: {selector: '.section .leftSection'},
rightRegion: {selector: '.section .rightSection'}
},
onShow: function() {
this.leftRegion.show(new CompositeView(this.model.get('left')));
this.rightRegion.show(new CompositeView(this.model.get('right')));
}
});
我得到的错误是:
Uncaught TypeError: Cannot read property 'apply' of undefined
方法
serializeModel: function(model) {
return model.toJSON.apply(model, _.rest(arguments));
}
在此行中触发:
this.headerRegion.show(new SectionLayoutView({model: this.model.get('headerModel')}));
答案 0 :(得分:3)
因为您是否将普通对象传递给视图...
this.headerRegion.show(new SectionLayoutView({
model: this.model.get('headerModel') // NOT a Backbone.Model
});
试试这个:
this.headerRegion.show(new SectionLayoutView({
model: new Backbone.Model(this.model.get('headerModel'))
});