【问题标题】:Another Dynamic Segments issue in Ember.jsEmber.js 中的另一个动态段问题
【发布时间】:2014-11-24 17:19:33
【问题描述】:

我的角色资源上有一个嵌套资产资源。我已经阅读了所有内容,所以我应该能够使用this.modelFor 来获取关联的父模型,但是当我使用它时,我的结果是未定义的。我哪里错了?

models/character.js

export default DS.Model.extend({
  assets: DS.hasMany('asset', { async: true }),
  ...
});

models/asset.js

export default DS.Model.extend({
  character: DS.belongsTo('character', { async: true }),
  ...
});

路由器.js

Router.map(function() {
  this.resource('characters', function () {
    this.route('show', { path: '/:character_id'} , function() {
      this.resource('assets', function() {

      });
    });
  });
  ...
});

routes/character/show.js

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('character', params.character_id);
  }
});

路由/资产/index.js

export default Ember.Route.extend({
  model: function() {
    return this.modelFor('character').get('assets');
  }
});

模板/字符/show.hbs

<div class="row">
  <h2>{{name}}</h2>
  {{#link-to 'assets' this}}Assets{{/link-to}}
</div>

由于 this.modelFor('character') 返回未定义:

Error while processing route: assets.index Cannot read property 'get' of undefined TypeError: Cannot read property 'get' of undefined

我的路线看起来和我预期的一样,给我一个 /chararcters/:charater_id:/assets 路线:

【问题讨论】:

    标签: javascript ruby-on-rails ember.js


    【解决方案1】:

    您只能获取具有模型的路线的模型。在您的路线布局中,只有 characters.show 路线实际上有一个模型。这就是您应该尝试从中获取模型的路线

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2012-05-10
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多