diff --git a/lib/datasource.js b/lib/datasource.js index c55ed685..6f3a088c 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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); } - } + }); } };