# Local Changes Being Overwritten By store.query()

**URL:** <https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454>\
**Category:** Ember Data\
**Created:** [January 19, 2016, 9:34pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454 "2016-01-19T21:34:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad\_julian](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@brad\_julian](https://discuss.emberjs.com/u/brad_julian)\
**Post date:** [January 19, 2016, 9:34pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/1 "2016-01-19T21:34:20Z")

</div>

Hi, all -

I’m working on a project where I am making local changes to a model that exists on my server. As I’m making these changes (which I don’t want to persist yet) I am fetching a list of this type of model using store.query(). The problem is, the results of the query are overwriting my local changes. I’ve been able to gather that this is the intended behavior, but is there any possible way to override it? If not, can I accomplish this use case at all?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![skaterdav85](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/skaterdav85/32/18291_2.png) [@skaterdav85](https://discuss.emberjs.com/u/skaterdav85)\
**Post date:** [January 19, 2016, 9:40pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/2 "2016-01-19T21:40:09Z")

</div>

What if you created a copy of the record you want to edit and edit that so that the original model, if changed from your store.query() call, doesn’t override your local changes? When you are finished editing the copy, merge those updates into the actual model in the store.

---

<div class="post-metadata">

**Author:** ![brad\_julian](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@brad\_julian](https://discuss.emberjs.com/u/brad_julian)\
**Post date:** [January 19, 2016, 9:43pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/3 "2016-01-19T21:43:29Z")

</div>

If I retrieve that “copy” in the model hook of a route, will it still updated dynamic segments in the URL as expected?

---

<div class="post-metadata">

**Author:** ![skaterdav85](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/skaterdav85/32/18291_2.png) [@skaterdav85](https://discuss.emberjs.com/u/skaterdav85)\
**Post date:** [January 19, 2016, 9:57pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/4 "2016-01-19T21:57:11Z")

</div>

Probably not. Does editing the actual model update dynamic segments in the URL currently?

---

<div class="post-metadata">

**Author:** ![workmanw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/workmanw/32/14929_2.png) [@workmanw](https://discuss.emberjs.com/u/workmanw)\
**Post date:** [January 19, 2016, 10:15pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/5 "2016-01-19T22:15:33Z")

</div>

@brad_julian Um, where did you read that this was intended behavior. Because as far as I know it’s not intended behavior. I created two JSBins to test this and it seems the changed values are preserved, while unchanged values are updated. IE `push` will patch the models. This is the behavior I expect to happen.

Ember-Data v1.13.15: [JS Bin - Collaborative JavaScript Debugging](http://emberjs.jsbin.com/jewuhadome/1/edit?html,js,output)   
Ember-Data v2.2.1: [JS Bin - Collaborative JavaScript Debugging](http://emberjs.jsbin.com/lucuyalani/1/edit?html,js,output)

You can see this by clicking `SetValue` and then clicking `PushValue`. The `pushCount` continues to update, but the `value` remains unchanged because it’s been overridden locally.

---

<div class="post-metadata">

**Author:** ![brad\_julian](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@brad\_julian](https://discuss.emberjs.com/u/brad_julian)\
**Post date:** [January 19, 2016, 11:24pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/6 "2016-01-19T23:24:41Z")

</div>

@workmanw,

Interesting. I thought it was the intended behavior because I misread some tests in the EmberData source. The property on my model that is being overridden has a custom Transform…could _that_ be the cause of my issue?

EDIT: I just confirmed that running `model.changedAttributes()` returns an empty object. I guess my question becomes, why doesn’t Ember Data recognize the changes I’m making to that attribute?

---

<div class="post-metadata">

**Author:** ![workmanw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/workmanw/32/14929_2.png) [@workmanw](https://discuss.emberjs.com/u/workmanw)\
**Post date:** [January 20, 2016, 12:12am UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/7 "2016-01-20T00:12:47Z")

</div>

@brad_julian I think you should try to make an ember-twiddle or JSBin that demonstrates your issue. 9/10 when I start to make a twiddle to show someone else I figure out my problem in the process. If that doesn’t do it, share it here and I’ll be happy to take a look.

---

<div class="post-metadata">

**Author:** ![brad\_julian](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@brad\_julian](https://discuss.emberjs.com/u/brad_julian)\
**Post date:** [January 20, 2016, 8:22pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/8 "2016-01-20T20:22:49Z")

</div>

Finally figured it out…

My property is an array with nested objects. Adding an object to the array, or changing a property of an object in the array does _not_ add the property to `model.changedAttributes()`. The only way to guarantee that is to clone the property value, perform the change, and then `model.set('propertyName', newValue)`. This seems really verbose, so I’m looking into how I might get around that now, but at least I understand the root cause.

Thanks for all your help!

---

<div class="post-metadata">

**Author:** ![workmanw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/workmanw/32/14929_2.png) [@workmanw](https://discuss.emberjs.com/u/workmanw)\
**Post date:** [January 20, 2016, 8:52pm UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/9 "2016-01-20T20:52:14Z")

</div>

Oh awesome! I’ll take this time to plug [ember-data-model-fragments](https://github.com/lytics/ember-data-model-fragments). If you need to support nesting of embedded records it’s a great addon. I’ve been using it in production for a year or so.

---

<div class="post-metadata">

**Author:** ![brad\_julian](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@brad\_julian](https://discuss.emberjs.com/u/brad_julian)\
**Post date:** [January 21, 2016, 1:52am UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/10 "2016-01-21T01:52:24Z")

</div>

While that looks like a really cool add-on, I can’t use it because my model structure looks like:

```
FooModel = DS.Model.extend({
  bar: DS.attr('barType') // barType is an array of Baz Models
});

BazModel = DS.Model.extend({ // <- this could be a fragment
  object: DS.belongsTo('audience') // <- except then this doesn't work
});

```

Unless I’m misreading the documentation, it doesn’t seem to support such a structure.

---

<div class="post-metadata">

**Author:** ![workmanw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/workmanw/32/14929_2.png) [@workmanw](https://discuss.emberjs.com/u/workmanw)\
**Post date:** [January 21, 2016, 6:02am UTC](https://discuss.emberjs.com/t/local-changes-being-overwritten-by-store-query/9454/11 "2016-01-21T06:02:44Z")

</div>

Yea you’re right, you won’t be able to use it.

One other thing you might take a look at is [DS.EmbeddedRecordsMixin](http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html). I’ve never used it, but I see others talk about it working well for them. Just a thought. Good luck!
