Save Penta and Baby immediately in Ember-Data

While playing with ember, I tried to save both the parent and the child at the same time. I noticed that the child parent_id was always set to nil.

Then I saw this problem on GitHub. It seems that the functionality has been removed from Ember-Data, but will be removed later. Until then, we're going to flip our own adapter to do this.

Reading @ tomdale's answer doesn't seem like it would be hard to implement, but I have a few implementation questions.

How can you determine when records were associated with an association? And which transition hook will go through?

Thanks!

+4
source share
1 answer

The store tracks changes in relationships with special objects. You can ask the store for a change object for this relationship:

store.relationshipChangeFor(child.get('clientId'), belongsToAssociationName); 

If no changes were made, it will not return anything.

But you will probably never need this. In the case of a relational data warehouse, a child record will be responsible for maintaining its associations. If you successfully create / update, you can blindly mark all these relations as allowed, and the store does not care if some of them were not actually dirty.

In fact, exactly what DS.RESTAdapter now:

 DS.RESTAdapter = DS.Adapter.extend({ // ... didSaveRecord: function(store, record, hash) { record.eachAssociation(function(name, meta) { if (meta.kind === 'belongsTo') { store.didUpdateRelationship(record, name); } }); store.didSaveRecord(record, hash); } // ... }); 

Whenever you decide that you are ready to mark the relationship as resolved, simply call store.didUpdateRelationship(child, belongsToAssociationName) .


All that is said, the real trick is not to define the relationship as allowed (since it has already been implemented) - it is to postpone the saving of the child until the parent is created.

We actually sent a transfer request that does just that. If this helps you to the extent that it helps us, we will be grateful for the support upon request.

0
source

Source: https://habr.com/ru/post/1442228/


All Articles