Thanks for your effort @gerrit. Should your second line be like this?
if ( snapshot.record['_inFlightAttributes'][key] != null
The problem here is that _inFlightAttributes contains only regular attributes, not relationships. In my example above, i am overriding the methods youâve pointed out to, i am not using _inFlightAttributes explicitly to keep bodies of serialize* methods uniform, but if you look at hasKeyChanged, you will see that this hash is used there, it is just not enough
Here is my revised solution that works with Ember CLI version 0.2.7, Ember version 1.13.0, and Ember Data version 1.13.4
Add the following to your serializer:
serializeAttribute: function(snapshot, json, key, attributes) {
// Check if new record
if (snapshot.get('isNew')) {
// Is new
return this._super(snapshot, json, key, attributes);
} else {
// Check if current attribute is in flight
let attrKey = '_internalModel._inFlightAttributes.'+key;
if (snapshot.get(attrKey) != null) {
// Attribute is dirty
return this._super(snapshot, json, key, attributes);
} else {
// Attribute is not dirty, not stored in inFlightAttributes
// Ignore this attribute
return;
}
}
I am aware of the serializeBelongsTo hook, but DS modelâs changedAttributes method doesnât keep track of belongsTo model changes. Which means there is no way for me to send the belongsTo attributes only when they change, instead I have to send all belongsTo association.
Assume there is a DS model for post like the one below
Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.belongsTo('user')
});
Assume that post one has user with id 1 as author. Now if I change author to user with id 2, changedAttributes methods doesnât contains details of the belongsTo association changed
i.e.
post = Post.find(1);
post.changedAttributes(); // Object {}
user = User.find(2);
post.set('user', user);
post.changedAttributes(); // still Object {}
P.S: I dont want the post to keep track changes in any of the user attributes, but I want to know changes in relationships if the Postâs author is pointing to new User model.
But ended building out something similar to the first answer in this stack overflow question, another property on the model called isDeepDirty. Its very old, and needs a bit of tinkering. Hope this helps.
Hi all, I use the ember-data-change-tracker addon to accomplish this. Simple install, activate in model and add the keep only changed mixin to the required model serializer, all outgoing requests will now only send attributes that have changed.