Ember-data: handling composition


I’m still search for a solution how to implement composition with ember-data in a clean way.

An easy example for this use case is an invoice with multiple lines (e.g. like in https://emberigniter.com/saving-models-relationships-json-api/ ). It should be possible to create a new invoice, add lines and store the whole thing at once. Additionally it is possible to edit the invoice and add and delete lines, but the change should be done just when the save button is clicked and not if the delete button on a line is pressed or a new line is added. Additionally the ids for the lines are created in the backend.

I expected to find an easy solution for this common use case, but I didn’t yet.

A workaround could be the usage of fragments, but because the lines are real database records with an unique id, this seams not to be the best matching solution. Other way round with real ember records makes it difficult to implement the create/update/delete just when the invoice is stored.

Has someone an other suggestion?

This is totally up to your API implementation. Ember-data implements JSON API Specification as default. This one does not support transactions yet. There is a proposal for upcoming 1.1 but ember-data is not supporting that one (yet). If there isn’t any decision yet about how your API should support transaction but current implementation is compliant to JSON API specification, I would recommend implementing that proposal. There is a good chance that it will ship to next version of the spec. I’m not aware of any addon providing support for it to be honest. It’s definitely some work to do but the community will be thankful for sure.