[英]How to handle associations between records in ember-data
Working with ember-data, a lot of different scenarios pop up that don't all seem to match the way the internals work at this point. 在处理余烬数据时,会弹出许多不同的场景,这些场景似乎都与此时内部结构的工作方式不匹配。 First off, some data: 首先,一些数据:
{ "post":
{
"id": "60",
"title": "Hello, Stack Overflow friends!",
"comments": []
}
}
Say the above is data from the database. 说上面的是来自数据库的数据。
Then, a post
record is fetched on client 1 and client 2 by calling post = App.Post.find(60)
on each client, respectively. 然后, 分别通过在每个客户端上调用post = App.Post.find(60)
来在客户端1和客户端2上获取post
记录。 So far, they both have the same post
record - loaded with an empty comments
array. 到目前为止,它们都具有相同的post
记录-加载了一个空的comments
数组。
At a later time, a comment is created on client 2 . 稍后,将在客户端2上创建评论。 This is done by calling comment = App.Comment.createRecord({text: "Why, hello there.", post: post})
. 这是通过调用comment = App.Comment.createRecord({text: "Why, hello there.", post: post})
。
The comment data is then saved server-side, looking like so: 然后将注释数据保存在服务器端,如下所示:
{ "comment":
{
"id": "80",
"text": "Why, hello there.",
"post_id": "60"
}
}
At this point, client 2 is up-to-date - since this is where the comment was created - but client 1 is out-of-sync , because it does not know of the new comment. 此时, 客户端2是最新的 -因为这是创建注释的地方-但客户端1是不同步的 ,因为它不知道新注释。
Now, client 1 may become aware of the newly created comment one way or another (via XHR or WS). 现在,客户端1可以通过一种方式(通过XHR或WS)了解新创建的注释。
Once client 1 knows the id, a comment
record is fetched by calling comment = App.Comment.find(80)
. 客户端1知道ID后,即可通过调用comment = App.Comment.find(80)
来获取comment
记录。
... Yet calling post.get('comments')
on client 1 results in 0 comments. ...但是在客户端1上调用post.get('comments')
产生0条评论。 Even though the new comment was fetched successfully, no association between the comment
and the post
was made. 即使成功获取了新评论, comment
与post
之间也没有关联。
comment
record with the post
record. 在客户端1上获取评论时,没有自动关联魔术将comment
记录与post
记录相关联。 Note 1: this does not happen because on client 1, the post
record was originally loaded with comments: []
. 注意1:不会发生这种情况,因为在客户端1上,该post
记录最初加载了comments: []
。 If the array had contained the comment id, 80
, this would have worked (besides the fact that the comment did not exist at load time). 如果数组包含注释id 80
,则该数组将起作用(除了在加载时注释不存在的事实)。
Note 2: I can add the association manually by calling post.get('comments').addObject(comment)
on client 1, but this dirties the post
record and doesn't seem like a proper way of handling this. 注意2:我可以通过在客户端1上调用post.get('comments').addObject(comment)
来手动添加关联, post.get('comments').addObject(comment)
post
记录,而且似乎不是处理此问题的正确方法。
post
and comment
records on client 1 that does not involve using addObject
or any similar functions that dirties the post
record? 有没有办法以某种方式在客户机1上的post
和comment
记录之间创建关联,而不涉及使用addObject
或addObject
post
记录的任何类似函数? When you load a record that has a belongsTo
relationship, ember data doesn't currently update the "parent" record of the relationship. 当您加载具有belongsTo
关系的记录时, belongsTo
烬数据当前不会更新该关系的“父”记录。
One way to solve this problem in your case is to sideload the "parent" record. 解决您的问题的一种方法是侧加载“父”记录。 In your case, you would send (via XHR or WS) the comment
and the post
. 对于您的情况,您将通过XHR或WS发送comment
和post
。
There's an open ticket for this problem https://github.com/emberjs/data/pull/695 有针对此问题的公开票证https://github.com/emberjs/data/pull/695
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.