EmberJS 2.7断言无法传递嵌套对象的id

时间:2016-08-23 11:30:40

标签: ember.js ember-router htmlbars

EmberJS 2.7 错误: 断言失败:传递给id的{​​{1}}必须是非空字符串或数字

应用/模板/产品/ index.hbs: [模型内部/每个循环我有这一行]:

findRecord()

应用/ router.js: [我定义了这些路线]:

{{#link-to 'categories.edit' product.category.id}}<a href="">{{product.category.name}}</a>{{/link-to}}

在编辑产品时有效。但在尝试编辑类别时会抛出上述错误。

如果我删除了&#39;类别/编辑&#39;路线并添加此路线:

  this.route('products', function() {
    this.route('new');
    this.route('edit', { path: '/:product_id/edit' });
  });

  this.route('categories', function() {
    this.route('new');
    this.route('edit', { path: '/:category_id/edit' });
  });

并更改模板以使用:

this.route('category', { path: '/categories/:category_id/edit' });

然后它有效。我理解为什么第二个有效。 但为什么第一个不起作用?

编辑:以下是模型

应用程序/模型/ product.js:

{{#link-to 'category' product.category.id}}<a href="">{{product.category.name}}</a>{{/link-to}}

应用程序/模型/ category.js:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  category: DS.belongsTo('category', { async: true })
});

1 个答案:

答案 0 :(得分:1)

固定。将findRecord放在模型上的位置很重要。我在类别路线中有这个代码。将其移动到正确的文件可以解决问题。

应用程序/路由/ categories.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model(params) {
        return this.store.findRecord('category', params.category_id);
    },
});

<强>解

应用程序/路由/ categories.js:

import Ember from 'ember';

export default Ember.Route.extend({
});

应用程序/路由/类别/ edit.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model(params) {
        return this.store.findRecord('category', params.category_id);
    },
});