0
votes

In my application, I search for documents with query. Then I edit one attribute in a single document, and then I call search query again.

Result is OK, I see document still in a dirty state with changed attribute.

Then I again pick one of documents and edit its hasMany relation (from 2 items to 4 items). And then I call search query again. Result is NOT OK, hasMany relation change is lost/disposed/rollbacked.

Is there a way so Ember query (i guess it's some Ember internal reload) does not rollback not saved relation changes ?

I am using Ember 2.9.1

For now i have no other way than prohibit any filter query actions or route actions anything that could call query again, since that would cause lost data that user set.

1

1 Answers

1
votes

Ember's store.query method always refetches the models from the backend (unlike the find* methods). With a query, it's the only way to make sure you have the most up-to-date data (and that the models loaded into the store are still valid). Even in your instance, you may run into unexpected results if you change the data such that it no longer meets your query criteria.

If you would like to keep edits around between queries, I would recommend making a copy of all models which are dirty (check the hasDirtyAttributes attribute). You can gather them with peekAll. Once copied, you can then make the query and patch the records by ID. Perhaps by using Ember.assign.

Even using that method, I would still think that you will get into trouble tracking the changes and making sure the records stay consistent with your query. Like what do you if that record wasn't returned (deleted on the server or no longer meets your criteria)? Do you drop your edits? What if you have a conflict between the data from the server and your local version (e.g. another user patched the hasMany relationship that the other user is now querying and modifying)?

ember-changeset seems like it could be useful here. However, it's not obvious how to get a ChangeSet to apply to a new instance of the same model. Though it may be possible to keep a snapshot and match them up manually. However, you still run into data consistency issues.