-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
changedAttributes doesn't return changed relationships #3045
Comments
IMO changing a belongs to association should show up in changedAttributes because the foreign_key would have changed. |
It's specifically called changed attributes, there will be a specific one for relationship pretty soon, but it is very not trivial. Maybe we can get away with partial solutions for a while. |
@igorT given the following example.
Doesn't it make sense that calling the Currently the save action would not fire because changing the address doesn't mark the model dirty even though the foreign key changed. Am I missing something? Is belongs to not relational with a foreign key attribute? |
@igorT, @jamesarosen, it keeps getting harder and harder to track if a belongs_to relationship has changed. I am not talking about testing for changes on the attributes of related models just testing if say a In rails terms Is there a current work around as the |
I don't know if this helps but I extended changedAttributes to also return changed relationships. I also included a snippet to commit without using the .save() method. Model.reopen({
/**
* commit() saves changes locally without pushing to the server
*/
commit() {
this.get('_internalModel').send('willCommit');
this.set('_internalModel._attributes', {});
this.get('_internalModel').send('didCommit');
},
/**
* changedAttributes() extends changedAttributes() to also return changed relationships
*/
changedAttributes() {
const attributes = this._super(...arguments);
const relationships = {};
// check relationships
this.eachRelationship((name, meta) => {
if (meta.kind === 'belongsTo') {
if (this.get(`_data.${name}.id`) !== this.get(`${name}.id`)) {
relationships[name] = [
this.get(`_data.${name}.id`),
this.get(`${name}.id`),
];
}
}
});
return Ember.merge(attributes, relationships);
},
}); |
@leifdejong thank you, this helped me alot |
@leifdejong why you check if meta.kind === 'belongsTo', what is with 'hasMany'? |
@igorT Any reason this got closed? this is still a legitimate issue and something like |
@leifdejong solution didn't work for me, so I modified it to get all up in those private apis bidness:
|
Can confirm @leifdejong solution is broken for me too (was previously working though). Really wish I didn't have to resort to using private api's for this sort of stuff... @atomkirk's solution above works in most cases, although I did have an issue where on calling it the first time on a model that has been retreived through findRecord, with no changes made, it would return something like; But then if I called So not sure what's up there. Stepping through the code it seems that the line that sets This solution also exists (but I haven't tried it) |
Currently,
changedAttributes
relies on comparing_data
and_attributes
._attributes
doesn't include any information about (belongs-to) relationships, so changing one won't show up inchangedAttributes
.It's totally reasonable that
changedAttributes
should not return relationship information because of its name. In that case, there should be a separatechangedRelationships
to match it.The text was updated successfully, but these errors were encountered: