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

Model's property observer is triggered after save although value isn't changed by response #2937

Closed
olivierlesnicki opened this issue Mar 25, 2015 · 9 comments

Comments

@olivierlesnicki
Copy link

I have an observer on a model's property.

When the model is saved, and only if the API returns the property (unchanged) as part of the payload, then the observer is being triggered.

Since the property hasn't changed - same type, same value - I'm left confused as to why my observer is being triggered after save.

@fivetanley
Copy link
Member

Can you post an example model definition and payload?

@olivierlesnicki
Copy link
Author

Note, this is similar to:
#2334 (comment)

@stefanpenner do we have an ETA for this?

@fivetanley
Copy link
Member

Most of the attrs/relationships don't use Ember Computed Properties for performance reasons, so I don't think this is similar to the other issue.

@olivierlesnicki
Copy link
Author

model/lead

import DS from 'ember-data';
export default DS.Model.extend({
  accountNumbers: DS.attr('string',{defaultValue: ''}),
});

request

{
  "lead": {
    "accountNumbers": "911000319219,851002826770,851002826771"
  }
}

response

{
  "status": "SUCCESS",
  "data": {
    "lead": {
      "id": "xxxxxx",
      "accountNumbers": "911000319219,851002826770,851002826771"
    }
  },
  "errors": {},
  "meta": {}
}

@olivierlesnicki
Copy link
Author

The observer on the accountNumbers get triggered after save. Although console.logging the value gives exactly the same.

Also, removing the accountNumbers from the response payload stop the observer being called again.

@wecc
Copy link
Contributor

wecc commented Mar 25, 2015

I can see why this is happening. When you set attributes on a record we store those changes internally in _attributes. However, in setupData() we call mergeAndReturnChangedKeys() with the newly received data and the canonical _data, not _attributes.

See #2649 (comment)

I still have a vision™ of completing my branch fixing this...

@olivierlesnicki
Copy link
Author

Does it mean that _data is an empty object just before save?

@wecc
Copy link
Contributor

wecc commented Mar 26, 2015

Yes, if the record is newly created. _data contains canonical data that we receive from the server. _attributes contains the local changes to attributes.

@igorT
Copy link
Member

igorT commented Mar 28, 2015

Can you provide a failing test? I think this shouldn't be too hard to solve. I think we probably just need to move https://github.com/emberjs/data/pull/2649/files#diff-4cc9f4885e8425eeaa555ef3648588aeR866 couple lines up

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

Successfully merging a pull request may close this issue.

4 participants