diff --git a/lib/dao.js b/lib/dao.js index 7f51ab90..4ed71b3e 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1235,8 +1235,12 @@ var defineScope = require('./scope.js').defineScope; * @param {ModelClass} [targetClass] The model class for the query, default to * the declaring model */ -DataAccessObject.scope = function (name, query, targetClass) { - defineScope(this, targetClass || this, name, query); +DataAccessObject.scope = function (name, query, targetClass, methods, options) { + var cls = this; + if (options && options.isStatic === false) { + cls = cls.prototype; + } + defineScope(cls, targetClass || cls, name, query, methods, options); }; /* diff --git a/lib/datasource.js b/lib/datasource.js index d710daa5..b488ce72 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -378,7 +378,7 @@ function isModelDataSourceAttached(model) { DataSource.prototype.defineScopes = function (modelClass, scopes) { if(scopes) { for(var s in scopes) { - defineScope(modelClass, modelClass, s, scopes[s]); + defineScope(modelClass, modelClass, s, scopes[s], {}, scopes[s].options); } } }; diff --git a/lib/scope.js b/lib/scope.js index 7ee30b3f..0c5ea86c 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -8,8 +8,9 @@ exports.defineScope = defineScope; exports.mergeQuery = mergeQuery; function ScopeDefinition(definition) { - this.modelFrom = definition.modelFrom || definition.sourceModel; - this.modelTo = definition.modelTo || definition.targetModel; + this.isStatic = definition.isStatic; + this.modelFrom = definition.modelFrom; + this.modelTo = definition.modelTo || definition.modelFrom; this.name = definition.name; this.params = definition.params; this.methods = definition.methods; @@ -80,7 +81,11 @@ function defineScope(cls, targetClass, name, params, methods, options) { } } + options = options || {}; + // Check if the cls is the class itself or its prototype + var isStatic = (typeof cls === 'function') || options.isStatic || false; var definition = new ScopeDefinition({ + isStatic: isStatic, modelFrom: cls, modelTo: targetClass, name: name, @@ -89,8 +94,13 @@ function defineScope(cls, targetClass, name, params, methods, options) { options: options || {} }); - cls.scopes = cls.scopes || {}; - cls.scopes[name] = definition; + if(isStatic) { + cls.scopes = cls.scopes || {}; + cls.scopes[name] = definition; + } else { + cls.constructor.scopes = cls.constructor.scopes || {}; + cls.constructor.scopes[name] = definition; + } // Define a property for the scope Object.defineProperty(cls, name, { diff --git a/package.json b/package.json index d1ba358b..a4e4fc46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "2.3.0", + "version": "2.3.1", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop",