ember-sync icon indicating copy to clipboard operation
ember-sync copied to clipboard

Should localStore be the source of truth?

Open tute opened this issue 11 years ago • 2 comments

When updating a record, the older version seems to persist in localStore. It happens like this:

  1. In models index, go to edit form for an object.
  2. Perform the update.
  3. Be redirected to index, model hook fires but the enqueued update of step 2 didn't yet arrive to the server.
  4. Fetch the index JSON object, with the not-yet-updated name.
  5. Where's my update?

Now if I retrigger the index model hook, or reload, the update is of course there. Should localStore be the source of truth, and push changes always but not pull? Should online find not fire if there's enqueued operations? I'm not sure how to deal with this issue yet.

Thank you for your time.

tute avatar Jul 07 '14 17:07 tute

An in-app workaround to query online only if there's no outstanding operations:

export default Ember.Route.extend({
  model: function() {
    if (Object.keys(this.queuedOps()).length) {
      return this.store.find("farm");
    } else {
      return this.emberSync.find("farm");
    }
  },

  queuedOps: function() {
    var queuedOps;
    queuedOps = JSON.parse(window.localStorage.getItem('namespace'));
    if (queuedOps && queuedOps.emberSyncQueueModel) {
      return queuedOps.emberSyncQueueModel.records;
    } else {
      return {};
    }
  }
});

tute avatar Jul 07 '14 18:07 tute

I like your approach, except that we shouldn't just check the queue. We should check if a particular item with same type and id is present. In that case, offlineStore takes precedence.

kurko avatar Jul 07 '14 23:07 kurko