1

我正在尝试保存与belongsTo自身有关系的记录(使用 ember-data)

App.Account = DS.Model.extend({
  name: DS.attr(),
  parent: DS.belongsTo(App.Account)
});

从服务器加载记录时,关系被正确加载。但是,当我尝试保存记录(创建或更新)时,belongsTo 记录没有更新。我正在尝试明确设置它:

a.set('parent', b);  (where a and b are both valid App.Account records)

查看通过网络发送的数据,该belongsTo属性未发送。我究竟做错了什么?我可以在belongsTo不指定关系的情况下设置hasMany关系吗?

4

3 回答 3

0

Not so much an answer as more info on the problem. I'm fighting a similar battle, and what I've found in my case is that when serialization happens the parent property is a DS.PromiseObject--this in itself wouldn't be a problem, but at the time serialization is happening it isn't isSettled…therefore, the Ember Data serialization process picks up undefined. The property is settled before and after the save, so I'm thinking that the fact that the parent object is being saved also and is refreshing its relationships (or something) is causing the child's parent property to become unsettled, leading to the problem.

Another part of this puzzle is that the Ember Data serializers actually serialize to a Javascript object, so you internally have an object like this: { "parent": undefined }, but when that object gets serialized to a string (likely by the browser's toJSON implementation?) the parent property gets dropped entirely--and that's what you see go across the wire. And I'm guessing that's why @Jeremy is observing what he's observing.

Now I just have to figure out how to fix it. Anyone???

于 2014-03-07T07:08:00.117 回答
0

据我所知,Ember 并不假设父密钥存储在某个belongsTo情况下的子节点上,而是假定父节点保留所有子节点的列表。(是的,这很奇怪......)我已经能够通过设置parentId : DS.attr('number')然后手动填充parentId. 不太理想,但它确实有效。

于 2013-09-20T05:52:56.777 回答
0

在 Ember Data Beta 2 中,您需要更改DS.belongsTo(App.Account)DS.belongsTo('account').

于 2013-09-19T18:49:13.300 回答