Ember BelongsTo 断言失败

Ember BelongsTo Assertion Failed

考虑以下 2 个模型:汽车和车主

我有当前车主 (bob jones) 的汽车记录 我有一个所有者记录 (bob jones),其中包含详细信息...地址、phone 等

在我的汽车表格(汽车模型)中,我有这个:

export default Model.extend({
    "owner_id": DS.belongsTo('owner'),
    "year": attr(''),
    "model": attr(''),
    "make": attr('')
})

DB端存储的是ID'12345',对应Bob Jones所有者记录。

当我加载汽车记录(2015 Jaguar)时,它还会连接到 Bob Jones 记录,它告诉我他的 phone、地址等。例如,如果我打印出

{{owner_id.id}} - {{owner_id.name}}

我会展示:

12345 - 鲍勃·琼斯

我的问题是,如果我更换车主会怎样。如果我 select 下拉列表

{{my-select
    name="owner_id"
    content=ownerList
    selection=owner_id.id
    prompt="select an owner"
    required=true
}}

来自这个组件:

import Ember from 'ember';

export default Ember.Component.extend({
  content: [],
  prompt: null,
  optionValuePath: 'value',
  optionLabelPath: 'label',

  init: function () {
    this._super(...arguments);
    if (!this.get('content')) {
      this.set('content', []);
    }
  },

  actions: {
    change: function () {
      let selectedIndex = this.$('select')[0].selectedIndex;
      let content = this.get('content');

      // decrement index by 1 if we have a prompt
      let hasPrompt = !!this.get('prompt');
      let contentIndex = hasPrompt ? selectedIndex - 1 : selectedIndex;
      let _selection = content[contentIndex];

      this.sendAction('willChangeAction', _selection);

      if (this.get('optionValuePath')) {
        this.set('selection', _selection[this.get('optionValuePath')]);
      } else {
        this.set('selection', _selection);
      }

      this.sendAction('didChangeAction', _selection);
    }
  }
});

并更改值...我收到此错误:

记录的id一旦处于加载状态就无法更改

如何更新 2015 Jaguar 汽车记录以更改车主,并加载新车主的详细信息?

您正在更改所有者的 ID,而不是更改美洲虎上的 owner_id 道具。我将 owner_id 属性更改为 owner 以帮助阐明 belongsTo 包含的内容。

我采取的一般策略是 select 车主,然后将其传回控制器以拯救汽车。

models/car.js

export default Model.extend({
    "owner": DS.belongsTo('owner'),
    // ...
})

routes/cars/car.js

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return Ember.RSVP.hash({
      car: this.store.find('car', params.id),
      ownerList: // however you get the owner list
    })
  }
})

controllers/cars/car.js

import Ember from 'ember';

export default Ember.Controller.extend({
  actions{
    ownerChanged(newOwner) {
      // If the select is setup properly, newOwner should be an instance of owner.
      const car = this.get('model.car');
      car.set('owner', newOwner);
      car.save
    }
  }
})

templates/cars/car.hbs。

{{my-select
    name="owner"
    content=model.ownerList
    selection=model.car.owner.id
    prompt="select an owner"
    required=true
    didChangeAction='ownerChanged'
}}

路线的模型挂钩中的代码大部分是为了清晰起见,如果您采用不同的方法,请相应地调整名称

您收到错误消息是因为更改模型的相关模型需要将关系属性设置为不同的模型,而不是更改相关模型的 ID。

xcskier56 是对的,您应该将您的关系声明为:

"owner": DS.belongsTo('owner')

这更有意义,因为现在您调用 car.owner.idcar.owner.name 而不是 car.owner_id.idcar.owner_id.name

现在如果你想改变一辆车的主人,你不能car.set('owner.id', newId);。您必须更改型号而不是 ID,如下所示:car.set('owner', newOwner);

并且要使用 select 更改所有者,您可以使用支持对象/模型作为选项的 select 插件 (ember-power-select) ,或者自己制作类似于 xcskier56 的建议。