保存belongsTo(没有hasmany) [英] Saving belongsTo (without hasmany)

查看:191
本文介绍了保存belongsTo(没有hasmany)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 belongsTo 保存记录(使用ember-data)

I'm trying to save a record (using ember-data) with a belongsTo relationship to itself

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

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

When loading the record from the server, the relationship is loaded correctly. However when I try to save the record (create or update), the belongsTo record isn't updated. I'm trying to set it explicitly:

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

查看发送的数据电线, belongsTo 属性未发送。我究竟做错了什么?我可以设置 belongsTo 关系,而不指定 hasMany 关系?

having a look at the data sent over the wire, the belongsTo attribute isn't sent. What am I doing wrong? am I able to set the belongsTo relationship without specifying a hasMany relationship?

推荐答案

没有太多的答案有关问题的更多信息。我正在打一场类似的战斗,而在我的情况下我发现,当序列化发生时,属性是一个 DS.PromiseObject - 这本身不会是一个问题,但是在序列化发生的时候,它不是 isSettled ...因此,Ember Data序列化流程拾取未定义。在保存之前和之后,属性 已经解决了,所以我认为,父对象被保存并且正在刷新它的关系(或某事)导致孩子的 parent 属性变得不安,导致问题。

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.

这个难题的另一部分是Ember Data序列化器实际上是序列化为Javascript 对象,所以你内部有一个这样的对象: {parent:undefined} ,但是当该对象被序列化为一个 string (可能是浏览器的 toJSON 实现?)属性完全删除 - 这就是你看到的电线。我猜这就是为什么@Jeremy正在观察他观察的内容。

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???

这篇关于保存belongsTo(没有hasmany)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆