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

View File

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