From 034830be864290fbc8bcc4dad94f97bcc57c02e4 Mon Sep 17 00:00:00 2001 From: Partap Davis Date: Mon, 2 Mar 2015 16:30:11 -0700 Subject: [PATCH] fix related.get() arguments to behave like related(cb) --- lib/relation-definition.js | 37 +++++++++++++++++++++++++++---------- lib/scope.js | 14 +++++++++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index b2b00b408..fb8f90ecb 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -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; @@ -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); } @@ -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; @@ -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) { @@ -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) { diff --git a/lib/scope.js b/lib/scope.js index 727da2b02..228e7e41c 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -73,6 +73,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres // Return from cache cb(null, self.__cachedRelations[name]); } + return cb.promise; } /** @@ -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;