-
-
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
Ember model.changedAttributes for unsaved record is not giving the default value. #4294
Comments
Oops, looks like you forgot to save the twiddle. Be sure to login into ember-twiddle and save via |
@akila1996 don't worry and no need to apologize 😉 I will take a look later but at a first sight this looks like a bug. Thanks for reporting! |
Looks related to #2566 |
model.changedAttributes() diffs the "server state" of the model with its "local state". You've created a brand new record that hasn't been saved yet, so as far as ED is concerned all previous attrs are undefined. The following sample produces similar behavior (not using defaultValue, just instantiating a person model with some default values): //models/person.js
export default DS.Model.extend({
name: DS.attr('string')
});
//anywhere where store is present
let person = this.store.createRecord('person', { name: 'Joe' });
person.set('name', 'Larry Smith');
person.changedAttributes(); // {"name":[undefined,"Larry"]} |
@xomaczar you are absolutely right! Thanks for reminding me how it works 😄 I've created #4324 to clarify this in the documentation. @akila1996 I am going to close this issue as it is actually not a bug... |
There is any way to get the behavior @xomaczar was expecting? |
I wasn't expecting anything different from what ED is currently doing - I was just trying to explain how it works and why. |
@xomaczar I'm quite new to ember and I'm looking for some function that identifies if the model was indeed changed. I'm using the solution from this post to identify and only send to server the changed fields. In my example: model.save().then(function() {
model.set('name', null); //clear the form
}); After that, if the user fill again the form with the same info, My goal is just clear the data after the form is sent but allow the user to send it again with the same info if he wants, and Is a little confusing, so please let me know if I wasn't clear enough. Thanks. |
@williamweckl changedAttributes() worked as expected. When you reset the name to null, right after you saved it, ED marks that model instance as dirty, if your user types the same value for the name - changedAttributes will not include that attr as part of changed attributes - because the canonical (a.k.a server state) of the model.name === local state of that model.name. I hope this makes sense. |
For an unsaved record with changed attributes , when used model.changedAttributes, the object returns the old value as undefined. Instead the old value should be the default value.
Hers is an example of the scenario.
https://ember-twiddle.com/?openFiles=controllers.application.js%2C
The text was updated successfully, but these errors were encountered: