【问题标题】:Marionette nested LayoutView - parsing modelMarionette 嵌套 LayoutView - 解析模型
【发布时间】:2015-06-17 14:11:45
【问题描述】:

我有 Marionette/Backbone 应用程序,它工作正常。我想在我们的视图中添加额外的层:

之前:

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 存在问题,但我不明白为什么。

【问题讨论】:

    标签: javascript backbone.js model marionette


    【解决方案1】:

    因为您将一个普通的对象传递给视图...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多