fix polymorphicName var scope

see https://github.com/strongloop/loopback-datasource-juggler/issues/271

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2014-08-31 19:57:37 +08:00
parent 7698f0f08b
commit d43044c766
1 changed files with 5 additions and 4 deletions

View File

@ -392,6 +392,7 @@ DataSource.prototype.defineScopes = function (modelClass, scopes) {
* @param relations
*/
DataSource.prototype.defineRelations = function (modelClass, relations) {
var self = this;
// Create a function for the closure in the loop
var createListener = function (name, relation, targetModel, throughModel) {
@ -428,7 +429,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
// Set up the relations
if (relations) {
for (var rn in relations) {
Object.keys(relations).forEach(function(rn) {
var r = relations[rn];
assert(DataSource.relationTypes.indexOf(r.type) !== -1, "Invalid relation type: " + r.type);
var targetModel, polymorphicName;
@ -447,12 +448,12 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
}
if (r.model) {
targetModel = isModelClass(r.model) ? r.model : this.getModel(r.model, true);
targetModel = isModelClass(r.model) ? r.model : self.getModel(r.model, true);
}
var throughModel = null;
if (r.through) {
throughModel = isModelClass(r.through) ? r.through : this.getModel(r.through, true);
throughModel = isModelClass(r.through) ? r.through : self.getModel(r.through, true);
}
if ((targetModel && !isModelDataSourceAttached(targetModel))
@ -469,7 +470,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
}
modelClass[r.type].call(modelClass, rn, params);
}
}
});
}
};