Skip to content
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

Closed
akila1996 opened this issue Apr 1, 2016 · 10 comments

Comments

@akila1996
Copy link

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

@pangratz
Copy link
Member

pangratz commented Apr 1, 2016

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
Copy link
Author

@pangratz
Copy link
Member

pangratz commented Apr 1, 2016

@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!

@pangratz
Copy link
Member

pangratz commented Apr 1, 2016

Looks related to #2566

@xomaczar
Copy link
Contributor

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

@pangratz
Copy link
Member

@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...

@williamweckl
Copy link

williamweckl commented Aug 19, 2016

There is any way to get the behavior @xomaczar was expecting?

@xomaczar
Copy link
Contributor

xomaczar commented Aug 19, 2016

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?

@williamweckl
Copy link

@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.

@xomaczar
Copy link
Contributor

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants