Local Changes Being Overwritten By store.query()

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!

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.

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

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

@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
Ember-Data v2.2.1: JS Bin - Collaborative JavaScript Debugging

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.

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

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

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!

Oh awesome! I’ll take this time to plug 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.

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.

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

One other thing you might take a look at is DS.EmbeddedRecordsMixin. I’ve never used it, but I see others talk about it working well for them. Just a thought. Good luck!