Managing transactions and relationships

I am trying to manage ember-data transactions (creating and committing records) on a model that has two belongsTo relationships. In initially I was simply adding the new record to the transactions using (in Coffeescript): @transaction.createRecord(App.Book, {author: App.router.authorController.get('content')})

This sets the author attribute (which is a DS.belongsTo relationship) to current author and then in a form the user sets the other attributes for the book, including another belongsTo relationship from a select element (let's say the publisher). This works fine, but if the user goes to create a second new record I get the following assertion raised:

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);

This is from line 203 of the one_to_many_change.js file in Ember Data

I take this to mean that I must manually add all the relevant model objects into the transaction. So I could add the author model to the transaction using something like:

author = App.router.authorController.get('content')
@transaction.add(author)

And then do the same for any publisher models that might be affected involved in the transaction (I was doing these operations within the console). However, when I do this I then get the following error:

Error: assertion failed: Once a record has changed, you cannot move it into a different transaction

This occurs even though the previous (original) transaction had been committed. Just wondering how others are dealing with this issues. I imagine assigning this kinds of relationships within transactions must be common process in Ember Data apps.


It looks like your author object was modified and attached to the default transaction. Then you create a new transaction and create a book on this one.

You have then 2 records that need to be persisted, both of them are linked through an association but they are on a different transaction.

One way of doing it would be to use the author transaction to create your book

var book = author.get('transaction').createRecord(App.Book, {author: author});
链接地址: http://www.djcxy.com/p/64070.html

上一篇: Ember模型生命周期,并恢复到原始状态

下一篇: 管理交易和关系