Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO: implement for when '.get(relation)' returns a Promise #31

Open
Leooo opened this issue Apr 10, 2015 · 1 comment
Open

TODO: implement for when '.get(relation)' returns a Promise #31

Leooo opened this issue Apr 10, 2015 · 1 comment

Comments

@Leooo
Copy link

Leooo commented Apr 10, 2015

I'm not sure in which cases ember doesn't returns promises for relations (it seems to be the standard behaviour with ember-data, even for async=false relationship), but in case it helps this is how I changed your code to work with promises (haven't checked if hasMany relationships work yet):

import RecordForSynchronization from 'ember-sync/record-for-synchronization';
import Ember from 'ember';

var alreadyRun=false;

export default {
  name: 'override-ember-sync',
  initialize: function() {
    if (alreadyRun) {
      return;
    } else {
      alreadyRun=true;
    }

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

        return new Ember.RSVP.Promise(function(resolve, reject) {
          record = _this.createRecordInStore();
          _this.setRelationships(record).then(function(rec){
            resolve(rec);
          });
        });
      },
      setRelationships: function(pendingRecord) {
        var _this = this,
            offlineRecord = this.get('offlineRecord'),
            type = this.get('jobRecord.jobRecordType');

        /**
         * 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});
        }
        var promises=[];
        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.
           */
          promises.push(offlineRecord.get(name).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);
              });
            }
            return pendingRecord;
          }));
        });
        return Ember.RSVP.all(promises).then(function(res){
          return pendingRecord;
        });
      },
    });
  },
}
@kurko
Copy link
Owner

kurko commented Jun 12, 2015

Any chance you could move this into a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants