kurko / ember-sync

MIT License
282 stars 28 forks source link

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

Open Leooo opened 9 years ago

Leooo commented 9 years ago

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;
            }
           //(..)
        });
      },
    });
kurko commented 9 years ago

Any chance you could open a PR with this code?