From 7fe7bf9b9a8c911355842f8d5700bbb3986accd5 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 8 Aug 2014 09:01:24 -0700 Subject: [PATCH] Add scope definitions to the model class See https://github.com/strongloop/loopback/issues/454 --- lib/scope.js | 21 +++++++++++++-------- test/scope.test.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 7fab605f..7ee30b3f 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -8,11 +8,12 @@ exports.defineScope = defineScope; exports.mergeQuery = mergeQuery; function ScopeDefinition(definition) { - this.sourceModel = definition.sourceModel; - this.targetModel = definition.targetModel || definition.sourceModel; + this.modelFrom = definition.modelFrom || definition.sourceModel; + this.modelTo = definition.modelTo || definition.targetModel; this.name = definition.name; this.params = definition.params; this.methods = definition.methods; + this.options = definition.options; } ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) { @@ -40,7 +41,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres || actualRefresh) { // It either doesn't hit the cache or refresh is required var params = mergeQuery(actualCond, scopeParams); - return this.targetModel.find(params, function (err, data) { + return this.modelTo.find(params, function (err, data) { if (!err && saveOnCache) { defineCachedRelations(self); self.__cachedRelations[name] = data; @@ -62,7 +63,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres * to return the query object * @param methods An object of methods keyed by the method name to be bound to the class */ -function defineScope(cls, targetClass, name, params, methods) { +function defineScope(cls, targetClass, name, params, methods, options) { // collect meta info about scope if (!cls._scopeMeta) { @@ -80,13 +81,17 @@ function defineScope(cls, targetClass, name, params, methods) { } var definition = new ScopeDefinition({ - sourceModel: cls, - targetModel: targetClass, + modelFrom: cls, + modelTo: targetClass, name: name, params: params, - methods: methods + methods: methods, + options: options || {} }); + cls.scopes = cls.scopes || {}; + cls.scopes[name] = definition; + // Define a property for the scope Object.defineProperty(cls, name, { enumerable: false, @@ -115,7 +120,7 @@ function defineScope(cls, targetClass, name, params, methods) { f._scope = typeof definition.params === 'function' ? definition.params.call(self) : definition.params; - f._targetClass = definition.targetModel.modelName; + f._targetClass = definition.modelTo.modelName; if (f._scope.collect) { f._targetClass = i8n.capitalize(f._scope.collect); } diff --git a/test/scope.test.js b/test/scope.test.js index ecaa8727..2060c80e 100644 --- a/test/scope.test.js +++ b/test/scope.test.js @@ -9,6 +9,14 @@ describe('scope', function () { db = getSchema(); Railway = db.define('Railway', { URID: {type: String, index: true} + }, { + scopes: { + highSpeed: { + where: { + highSpeed: true + } + } + } }); Station = db.define('Station', { USID: {type: String, index: true}, @@ -24,9 +32,15 @@ describe('scope', function () { Station.destroyAll(done); }); }); + + it('should define scope using options.scopes', function () { + Railway.scopes.should.have.property('highSpeed'); + Railway.highSpeed.should.be.function; + }); it('should define scope with query', function (done) { Station.scope('active', {where: {isActive: true}}); + Station.scopes.should.have.property('active'); Station.active.create(function (err, station) { should.not.exist(err); should.exist(station);