Skip to content

Commit

Permalink
fix related.get() arguments to behave like related(cb)
Browse files Browse the repository at this point in the history
  • Loading branch information
partap committed Mar 2, 2015
1 parent 6116b2c commit 034830b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
37 changes: 27 additions & 10 deletions lib/relation-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,10 +1394,10 @@ BelongsTo.prototype.related = function (refresh, params) {
cb(err);
}
});
return modelInstance[fk];
return cb.promise || modelInstance[fk];
} else {
cb(null, cachedValue);
return cachedValue;
return cb.promise || cachedValue;
}
} else if (params === undefined) { // acts as sync getter
return cachedValue;
Expand All @@ -1415,9 +1415,17 @@ BelongsTo.prototype.related = function (refresh, params) {
* @param {Function} cb Callback of the form function (err, inst)
* @returns {Promise | Undefined} returns promise if callback is omitted
*/
BelongsTo.prototype.get = function (cb) {
var cb = cb || utils.createPromiseCallback();
return this.related(true, cb);
BelongsTo.prototype.get = function (refresh, cb) {
if (cb === undefined && refresh === undefined) {
cb = utils.createPromiseCallback();
// only pass 1 argument
return this.related(cb);
} else if (cb === undefined) {
if (typeof refresh !== 'function') {
cb = utils.createPromiseCallback();
}
}
return this.related(refresh, cb);
}


Expand Down Expand Up @@ -1763,10 +1771,10 @@ HasOne.prototype.related = function (refresh, params) {
cb(err);
}
});
return modelInstance[pk];
return cb.promise || modelInstance[pk];
} else {
cb(null, cachedValue);
return cachedValue;
return cb.promise || cachedValue;
}
} else if (params === undefined) { // acts as sync getter
return cachedValue;
Expand All @@ -1784,9 +1792,17 @@ HasOne.prototype.related = function (refresh, params) {
* @returns {Promise | Undefined} Returns promise if cb is omitted
*/

HasOne.prototype.get = function (cb) {
var cb = cb || utils.createPromiseCallback();
return this.related(true, cb);
HasOne.prototype.get = function (refresh, cb) {
if (cb === undefined && refresh === undefined) {
cb = utils.createPromiseCallback();
// only pass 1 argument
return this.related(cb);
} else if (cb === undefined) {
if (typeof refresh !== 'function') {
cb = utils.createPromiseCallback();
}
}
return this.related(refresh, cb);
};

RelationDefinition.embedsOne = function (modelFrom, modelTo, params) {
Expand Down Expand Up @@ -2677,6 +2693,7 @@ ReferencesMany.prototype.related = function(receiver, scopeParams, condOrRefresh
var params = mergeQuery(actualCond, scopeParams);

modelTo.findByIds(ids, params, cb);
return cb.promise;
};

ReferencesMany.prototype.findById = function (fkId, cb) {
Expand Down
14 changes: 11 additions & 3 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
// Return from cache
cb(null, self.__cachedRelations[name]);
}
return cb.promise;
}

/**
Expand Down Expand Up @@ -186,9 +187,16 @@ function defineScope(cls, targetClass, name, params, methods, options) {
f._targetClass = i8n.camelize(f._scope.collect);
}

f.get = function (cb) {
var cb = cb || utils.createPromiseCallback();
return definition.related(self, f._scope, true, cb);
f.get = function (condOrRefresh, cb) {
if (cb === undefined && condOrRefresh === undefined) {
cb = utils.createPromiseCallback();
return definition.related(self, f._scope, cb);
} else if (cb === undefined) {
if (typeof condOrRefresh !== 'function') {
cb = utils.createPromiseCallback();
}
}
return definition.related(self, f._scope, condOrRefresh, cb);
}

f.build = build;
Expand Down

0 comments on commit 034830b

Please sign in to comment.