Ember-Data: Intended usage pattern of DS.EmbeddedRecordsMixin

I’ve got a backend, that let’s me read data asynchronously, but enforces embedding of certain child data (in lieu of transactions).

When using DS.EmbeddedRecordsMixin with …

{
  serialize: 'records',
  deserialize: 'ids'
}

… (which should be the right thing to do given this backend) I’ve still got two questions.

(1) http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html is very explicit, that DS.EmbeddedRecordsMixin shouldn’t be mixed with async:true. From what I understand, the problem would be mixing async:true with embedded reading, not writing. On the other hand the documentation doesn’t differentiate. Am I good with mixing async:true with above configuration (and is the documantion missing this case), or what is it that I am missing about async:true and writing?

(2) In my backend when deleting the parent, all children are delete as well. Deleting should be just one call for the parent. How do I do this on the ember end? I want to delete the parent and all children in the store, send one REST request and the success/failure of this request should be authoritative for all those records. Yet, the DS.EmbeddedRecordsMixin doesn’t seem to help me in any way in that direction. Am I on my own, or what am I missing?

Answer

As far as your first question goes, I think you are good with mixing async:true and the EmbeddedRecordsMixin for your case. See this blog post and my example js-bin

As for 2, EmbeddedRecordsMixin leaves you on your own for that. But if you look at the code, all it does is push the records into the store when extracting them, so to reverse it all you should have to do is unload those records from the store. I’d keep a reference to the child records, then on a successful delete of the parent, clean up the children with record.unload(). Same example js-bin

Leave a Reply

Your email address will not be published. Required fields are marked *