From 526d126e4181fde0ca8d15f3cd69005f02cbc6b6 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 11 Nov 2013 22:06:43 -0800 Subject: [PATCH] Fix the relation lazy setup --- lib/dao.js | 5 ++--- lib/datasource.js | 13 +++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index c8911985..6d3175da 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -791,11 +791,10 @@ var defineScope = require('./scope.js').defineScope; /** * Define scope */ -DataAccessObject.scope = function (name, filter) { - defineScope(this, this, name, filter); +DataAccessObject.scope = function (name, filter, targetClass) { + defineScope(this, targetClass || this, name, filter); }; - // jutil.mixin(DataAccessObject, validations.Validatable); jutil.mixin(DataAccessObject, Inclusion); jutil.mixin(DataAccessObject, Relation); diff --git a/lib/datasource.js b/lib/datasource.js index 3b95bf22..9854e36c 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -324,7 +324,7 @@ DataSource.prototype.defineRelations = function(modelClass, relations) { if (!isModelDataSourceAttached(targetModel)) { targetModel.once('dataSourceAttached', function (model) { // Check if the through model doesn't exist or resolved - if (!throughModel || !throughModel.settings.unresolved) { + if (!throughModel || isModelDataSourceAttached(throughModel)) { // The target model is resolved var params = { foreignKey: relation.foreignKey, @@ -337,11 +337,12 @@ DataSource.prototype.defineRelations = function(modelClass, relations) { modelClass[relation.type].call(modelClass, name, params); } }); + } if (throughModel && !isModelDataSourceAttached(throughModel)) { // Set up a listener to the through model throughModel.once('dataSourceAttached', function (model) { - if (!targetModel.settings.unresolved) { + if (isModelDataSourceAttached(targetModel)) { // The target model is resolved var params = { foreignKey: relation.foreignKey, @@ -482,9 +483,9 @@ DataSource.prototype.createModel = DataSource.prototype.define = function define return modelClass; } + this.setupDataAccess(modelClass, settings); modelClass.emit('dataSourceAttached', modelClass); - this.setupDataAccess(modelClass, settings); return modelClass; }; @@ -562,10 +563,10 @@ DataSource.prototype.attach = function (modelClass) { this.modelBuilder.definitions[className] = def; this.modelBuilder.models[className] = modelClass; - modelClass.emit('dataSourceAttached', modelClass); - this.setupDataAccess(modelClass, settings); + modelClass.emit('dataSourceAttached', modelClass); + return this; }; @@ -573,7 +574,7 @@ DataSource.prototype.attach = function (modelClass) { * Define single property named `prop` on `model` * * @param {String} model - name of model - * @param {String} prop - name of propery + * @param {String} prop - name of property * @param {Object} params - property settings */ DataSource.prototype.defineProperty = function (model, prop, params) {