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;
});
},
});
},
}
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):