Marionette嵌套LayoutView - 解析模型

时间:2015-06-17 14:11:45

标签: javascript backbone.js model marionette

我有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')}));

你可以告诉我任何有关错误的想法吗?我们在其他地方有类似的代码,它工作正常。将模型解析为json似乎存在问题,但我看不出原因。

1 个答案:

答案 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'))
});