Add scope definitions to the model class

See https://github.com/strongloop/loopback/issues/454
This commit is contained in:
Raymond Feng 2014-08-08 09:01:24 -07:00
parent 0adf440cd1
commit 7fe7bf9b9a
2 changed files with 27 additions and 8 deletions

View File

@ -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);
}

View File

@ -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},
@ -25,8 +33,14 @@ describe('scope', function () {
});
});
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);