Ember Data reloads associations when a response is included in a PUT request

I have the following simple relationships between parents and parents.

App.Parent = DS.Model.extend({ children: DS.hasMany('child') }); App.Child = DS.Model.extend({ parent: DS.belongsTo('parent') }); 

I have a situation where I update an instance of Child and save the changes using save() . This causes a PUT request. Usually a PUT request returns 204 No Content , but I return 200 OK with serializing the JSON model as an answer, for example:

 { child: { parent: 1 } } 

Unfortunately, this causes a reboot of the parent. So right after that, the GET request to /parents/1 is issued by Ember Data. How can I prevent this?

+5
source share
1 answer

Does it look like you're returning partial results?

I have not seen a suitable solution in interwebs that deals with partial updates in the right place. This may or may not solve your problem, but it may help those who have a similar problem due to partial results.

You can try to override extractUpdateRecord in your application serializer or in specific model serializers for cases when you return partial results.

Here is the default implementation:

 /** `extractUpdateRecord` is a hook into the extract method used when a call is made to `DS.Store#update`. By default this method is alias for [extractSave](#method_extractSave). @method extractUpdateRecord @param {DS.Store} store @param {subclass of DS.Model} type @param {Object} payload @param {String or Number} id @param {String} requestType @return {Object} json The deserialized payload */ extractUpdateRecord: function(store, type, payload, id, requestType) { return this.extractSave(store, type, payload, id, requestType); }, 

You will need to serialize the entry in JSON and then combine the payload data to update it. Something like the following:

 extractUpdateRecord: function(store, type, payload, id, requestType) { var record = store.getById(type, id); var currentData = record.toJSON(); var newData = this.extractSave(store, type, payload, id, requestType); return Ember.merge(currentData, newData); } 
+1
source

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


All Articles