Fix a name conflict in scope metadata

This commit is contained in:
Raymond Feng 2014-08-08 15:52:30 -07:00
parent a48d2fdfeb
commit c53dc74d16
4 changed files with 22 additions and 8 deletions

View File

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

View File

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

View File

@ -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, {

View File

@ -1,6 +1,6 @@
{
"name": "loopback-datasource-juggler",
"version": "2.3.0",
"version": "2.3.1",
"description": "LoopBack DataSoure Juggler",
"keywords": [
"StrongLoop",