保存belongsTo(没有hasmany)

编程入门 行业动态 更新时间:2024-10-25 18:33:37
本文介绍了保存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)

本文发布于:2023-10-21 17:18:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:belongsTo   hasmany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!