Fix the relation lazy setup

This commit is contained in:
Raymond Feng 2013-11-11 22:06:43 -08:00
parent 275bb6ffac
commit 526d126e41
2 changed files with 9 additions and 9 deletions

View File

@ -791,11 +791,10 @@ var defineScope = require('./scope.js').defineScope;
/** /**
* Define scope * Define scope
*/ */
DataAccessObject.scope = function (name, filter) { DataAccessObject.scope = function (name, filter, targetClass) {
defineScope(this, this, name, filter); defineScope(this, targetClass || this, name, filter);
}; };
// jutil.mixin(DataAccessObject, validations.Validatable); // jutil.mixin(DataAccessObject, validations.Validatable);
jutil.mixin(DataAccessObject, Inclusion); jutil.mixin(DataAccessObject, Inclusion);
jutil.mixin(DataAccessObject, Relation); jutil.mixin(DataAccessObject, Relation);

View File

@ -324,7 +324,7 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
if (!isModelDataSourceAttached(targetModel)) { if (!isModelDataSourceAttached(targetModel)) {
targetModel.once('dataSourceAttached', function (model) { targetModel.once('dataSourceAttached', function (model) {
// Check if the through model doesn't exist or resolved // Check if the through model doesn't exist or resolved
if (!throughModel || !throughModel.settings.unresolved) { if (!throughModel || isModelDataSourceAttached(throughModel)) {
// The target model is resolved // The target model is resolved
var params = { var params = {
foreignKey: relation.foreignKey, foreignKey: relation.foreignKey,
@ -337,11 +337,12 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
modelClass[relation.type].call(modelClass, name, params); modelClass[relation.type].call(modelClass, name, params);
} }
}); });
} }
if (throughModel && !isModelDataSourceAttached(throughModel)) { if (throughModel && !isModelDataSourceAttached(throughModel)) {
// Set up a listener to the through model // Set up a listener to the through model
throughModel.once('dataSourceAttached', function (model) { throughModel.once('dataSourceAttached', function (model) {
if (!targetModel.settings.unresolved) { if (isModelDataSourceAttached(targetModel)) {
// The target model is resolved // The target model is resolved
var params = { var params = {
foreignKey: relation.foreignKey, foreignKey: relation.foreignKey,
@ -482,9 +483,9 @@ DataSource.prototype.createModel = DataSource.prototype.define = function define
return modelClass; return modelClass;
} }
this.setupDataAccess(modelClass, settings);
modelClass.emit('dataSourceAttached', modelClass); modelClass.emit('dataSourceAttached', modelClass);
this.setupDataAccess(modelClass, settings);
return modelClass; return modelClass;
}; };
@ -562,10 +563,10 @@ DataSource.prototype.attach = function (modelClass) {
this.modelBuilder.definitions[className] = def; this.modelBuilder.definitions[className] = def;
this.modelBuilder.models[className] = modelClass; this.modelBuilder.models[className] = modelClass;
modelClass.emit('dataSourceAttached', modelClass);
this.setupDataAccess(modelClass, settings); this.setupDataAccess(modelClass, settings);
modelClass.emit('dataSourceAttached', modelClass);
return this; return this;
}; };
@ -573,7 +574,7 @@ DataSource.prototype.attach = function (modelClass) {
* Define single property named `prop` on `model` * Define single property named `prop` on `model`
* *
* @param {String} model - name of model * @param {String} model - name of model
* @param {String} prop - name of propery * @param {String} prop - name of property
* @param {Object} params - property settings * @param {Object} params - property settings
*/ */
DataSource.prototype.defineProperty = function (model, prop, params) { DataSource.prototype.defineProperty = function (model, prop, params) {