Data: Ember model.changedAttributes for unsaved record is not giving the default value.

Created on 1 Apr 2016  路  10Comments  路  Source: emberjs/data

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

Most helpful comment

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"]}

All 10 comments

Oops, looks like you forgot to save the twiddle. Be sure to login into ember-twiddle and save via File -> Save to GitHub Gist

@akila1996 don't worry and no need to apologize :wink:

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 :smile:

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.
@williamwecki what are you looking for? What's your use case?

@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, changedAttributes() is empty.

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 changedAttributes() does not do what I need. :(

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.
As for your use case/feature, I need more info or better a twiddle.com with an example or an animated gif - it's hard to visualize why you are setting a model's name to null right after you pushed it to the server. Would that not confuse your users who just typed 'Bob' into name field and hit a 'save' button to see a blank name? Surely not. Most likely I am not understanding the user flow you are trying to implement. Are you on slack - maybe we can move this conversion over there?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stefanpenner picture stefanpenner  路  4Comments

jlami picture jlami  路  3Comments

NullVoxPopuli picture NullVoxPopuli  路  5Comments

maschwenk picture maschwenk  路  5Comments

bekicot picture bekicot  路  4Comments