How can I ignore dirty entries when updating the list from the server?

Use the latest ember data and ember data.

I have a one-page application with a list of items and the ability to open items in tabs. I can edit elements on open tabs and not make a dirty recording, return to the list.

If I update the list, I get an error message:

Error: Attempted to handle event loadedData on <> while in state rootState.loaded.updated.uncommitted

This, of course, is because I did App.TestObject.find()on the list and still have dirty uncommitted entries (open and edited entries on tabs).

My goal is to show a list with updated entries, but to do nothing with unregistered entries. I do not want to roll back unregistered entries. Is there any best practice for this?

This is a similar question , but I do not want the records to return to their original state. This is a similar case with the violin , but here rollback is the right solution.

How can I solve the script if I want to ignore uncommitted entries when I return to the list?

+5
source share
2 answers

I have a workaround for this problem with monkey-patching DS.Model.

DS.Model.reopen({
  loadedData: function() {
    if (this.get('isDirty') === false) {
      this._super.apply(this, arguments);
    }
  }
});

The resulting model does not update itself in a dirty state, regardless of what is in the new JSON regarding this entry. The remaining entries will be updated very well.

+5
source

If you do not want monkey patch DS.Model.loadedData, here is another solution:

App.Contact.reopenClass({
    // Results of our find() call.
    cache: null,

    // Either find our data for the first time, or refresh what we have.
    findAllWithRefresh: function () {
        if (this.cache === null) {
            this.cache = this.find();
        } else {
            this.cache.forEach(function (c) {
                // This appears to be a correct list of conditions for calling
                // refresh(), but you may need to tweak it.
                if (c.get("isLoaded") && !c.get("isSaving") &&  !c.get("isError") && !c.get("isDeleted") && !c.get("isDirty") && !c.get("isReloading")) {
                    console.log("Refreshing", c);
                    c.reload();
                } else {
                    console.log("Can't refresh", c);
                }
            });        
        }
        return this.cache;
    }
});

App.ContactsRoute = Ember.Route.extend({
    model: function (params) {
        // Note that we won't see any new records using this approach.
        return App.Contact.findAllWithRefresh();  
    }
});

Here's the working jsFiddle .

, App.Contact.find(), . Ember Data.

+4

All Articles