Hi, I have a model with a lot of attributes. I need update just one. The ‘favorite’ attribute. I update the record executing the record ‘save’ method. The rest adapter will launch a http put request, with the entire model serialized into json. This causes me two problems.
The first problem is that I think it is not very efficient force to travel all data model when only i want to update one attribute.
The other problem is that on the server side, I need to know exactly what property has been modified. Lets say I have a method called ‘setFavorite’ that makes a quick update on the DB and another method that updates the entire model and I have to distinguish between the two cases.
Is there any way to do this with ember-data?
Ok, so you’re doing a request over http followed by a call to a database over the network. The size of the payload for each of these operations is completely insignificant compared to the wire time. On your server simply have a single operation that updates all the model’s attributes at once and you’re done.
Opsb, thanks for your answer. This was the approach we were thinking to do. But one of my team mates detected a problem. We have a list of groups.
We want to toggle a favorite flag. But the other group data, as name or description can be modified too. What if some user modifies for example the name, and other user that still have the older name in his client marks this group as favorite. As the server method updates the whole model, he would override the name with the old value.
We need to know at server side exactly which field has to be updated.
Ah, I see. That is indeed a tricky problem to solve with ember-data.
One approach would be to version your models on the server side and include the version number when you send the model to the client. Then when you save the model in ember-data the version number would be sent back to the server. When the server receives the update it could retrieve the corresponding version that was updated and do a diff with the new update (leaving you with only the changes attributes since that version). You then update the current version of the model(server side) with your diffed attributes, increment the version number and send this merged version (with the new version number) back in the response. Ember-data will then update the version number on the client so that you can rinse and repeat.
Looks like there’s another thread about this topic. They suggest customising the serializer to only send attributes that have changed to the server. Dirt tracking with Ember-Data - only save dirty properties - #3 by gerrit
This was exactly what I was looking for. I extended the rest serializer and it’s working fine. Thanks!