【发布时间】:2013-04-07 02:08:42
【问题描述】:
我正在尝试在具有两个 belongsTo 关系的模型上管理 ember-data 事务(创建和提交记录)。最初,我只是使用(在 Coffeescript 中)将新记录添加到交易中:
@transaction.createRecord(App.Book, {author: App.router.authorController.get('content')})
这会将作者属性(即 DS.belongsTo 关系)设置为当前作者,然后用户在表单中设置图书的其他属性,包括来自 select 元素(假设是出版商)的另一个 belongsTo 关系。这工作正常,但如果用户去创建第二条新记录,我会得到以下断言:
Ember.assert("All records in a changed relationship must be in the same transaction. You tried to change the relationship between records when one is in " + t + " and the other is in " + prev, t === prev);
这是one_to_many_change.js file in Ember Data的第203行
我认为这意味着我必须手动将所有相关模型对象添加到事务中。因此,我可以使用以下方式将作者模型添加到事务中:
author = App.router.authorController.get('content')
@transaction.add(author)
然后对交易中可能受影响的任何发布者模型执行相同的操作(我在控制台中执行这些操作)。但是,当我这样做时,我会收到以下错误:
Error: assertion failed: Once a record has changed, you cannot move it into a different transaction
即使先前的(原始)事务已提交,也会发生这种情况。只是想知道其他人是如何处理这个问题的。我想在交易中分配这种关系必须是 Ember Data 应用程序中的常见过程。
【问题讨论】:
-
我认为您在这里的断言:“即使先前(原始)事务已提交,也会发生这种情况”要么与 github.com/emberjs/data/pull/412 有关,要么是因为当您要添加时服务器响应尚未到达作者。您能否应用我在 PR 中建议的快速修复,并告诉我它是否有效?
-
@sly7_7 谢谢!该拉取请求似乎解决了我的问题......希望它很快就会被合并。我将对拉取请求添加评论。
-
不知道这个PR对不对,好像太简单了。但无论如何,用这里的链接更新它是一件好事:)
-
我一直在做的解决这个问题是在提交事务后调用 .removeCleanRecords() 。我认为当我们能够将脏记录移动到不同的事务时,这将不是问题。
标签: ember.js ember-data