Skip to content

Instantly share code, notes, and snippets.

@kurko
Last active August 29, 2015 14:14
Show Gist options
  • Save kurko/7ca3b17fa6b7590b93a1 to your computer and use it in GitHub Desktop.
Save kurko/7ca3b17fa6b7590b93a1 to your computer and use it in GitHub Desktop.

The code at https://github.com/kurko/ember-sync/blob/master/dist/ember-sync.js#L640-L666 would become something like this:

  setRelationships: function(pendingRecord) {
    var _this = this,
        offlineRecord = this.get('offlineRecord'),
        type = this.get('jobRecord.jobRecordType');
        
    // ====== new code =======
    return new Ember.RSVP.Promise(function(resolveRelationships, reject) {
      /**
       * We need to loop relationships. If no record was
       * passed in, we need to create a new one, fake, so we know about the
       * relationships.
       */
      if (!offlineRecord) {
        offlineRecord = this.onlineStore.push(type, {id: 1});
      }

      offlineRecord.eachRelationship(function(name, descriptor) {
        var key = descriptor.key,
            kind = descriptor.kind,
            type = descriptor.type,
            relation, onlineRelation;

        /**
         * TODO - implement for when `.get(relation)` returns a Promise.
         */
         
        // ====== new code =======
        relation = RSVP.resolve(offlineRecord.get(name));
        // I'm not sure if offlineRecord.get(name) is still the same in Ember Data,
        // it could have changed so beware. Here, `name` is the relationship name.
        relation.then(function(relation) {
        
          /**
           * We need to attach relationships to the main record. If the
           * relationships don't exist anymore offline, we need to generate a new
           * one, fake, with the same ID, just to send to the server.
           */
          if (kind == "belongsTo") {
            var relationId = _this.get('jobRecord.serialized')[name];

            if (relationId && !relation) {
              relation = _this.onlineStore.push(type, {id: relationId});
            }

            if (relation) {
              onlineRelation = _this.generateRelationForRecord(type, relation);
              pendingRecord.set(key, onlineRelation);
            }
          } else if (kind == "hasMany") {
            relation.forEach(function(relation) {
              onlineRelation = _this.generateRelationForRecord(type, relation);
              pendingRecord.get(name).pushObject(onlineRelation);
            });
          }
        });

        // ====== new code =======
        resolve(pendingRecord);
      });
    });
  },

Line 556, toEmberData would become something like:

  toEmberData: function() {
    var _this = this,
        record;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      record = _this.createRecordInStore();
      _this.setRelationships(record).then(function(record) {
        resolve(record);
      });
    });
  },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment