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

Don't override the offlineStore records with empty records created on the online store due to id-only relationships fetched

Open Leooo opened this issue 9 years ago • 1 comments

a post and many comments exist and are stored offline.

When refreshing the page, the offline models are created, and no online models exist.

If refreshing only the post from the server (and returning the list of ids for the comments), the online store will create empty records for the comments, as it is not aware of any existing comments yet. These empty comments are then pushed to the offline server, overriding the old, correct, comments.

A solution: don't push to collection associations when the corresponding models are empty:

    Record.reopen({
      _serializeAssociations: function() {
        var serializedCollection = [],
            _this = this,
            snapshot = this._snapshot();
        snapshot.eachRelationship(function(name, relationship) {
          var hasManyRecords = null;

          var pushToCollection = function(snapshot) {
             if (typeof(snapshot)==='undefined') {return true;}
             if (snapshot.get('isEmpty')) {
              //added: don't replace offline record..
              return true;
            }
           //(..)
        });
      },
    });

Leooo avatar Apr 30 '15 15:04 Leooo

Any chance you could open a PR with this code?

kurko avatar Jun 12 '15 00:06 kurko