Ensure the model is attached to DataSource for relations

This commit is contained in:
Raymond Feng 2013-11-09 22:16:32 -08:00
parent 7aa2eefec4
commit ec7f79e935
1 changed files with 7 additions and 3 deletions

View File

@ -308,6 +308,10 @@ function isModelClass(cls) {
DataSource.relationTypes = ['belongsTo', 'hasMany', 'hasAndBelongsToMany'];
function isModelDataSourceAttached(model) {
return model && (!model.settings.unresolved) && (model.dataSource instanceof DataSource);
}
/*!
* Define relations for the model class from the relations object
* @param modelClass
@ -317,7 +321,7 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
// Create a function for the closure in the loop
var createListener = function (name, relation, targetModel, throughModel) {
if (targetModel && targetModel.settings.unresolved) {
if (!isModelDataSourceAttached(targetModel)) {
targetModel.once('dataSourceAttached', function (model) {
// Check if the through model doesn't exist or resolved
if (!throughModel || !throughModel.settings.unresolved) {
@ -334,7 +338,7 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
}
});
}
if (throughModel && throughModel.settings.unresolved) {
if (throughModel && !isModelDataSourceAttached(throughModel)) {
// Set up a listener to the through model
throughModel.once('dataSourceAttached', function (model) {
if (!targetModel.settings.unresolved) {
@ -369,7 +373,7 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
throughModel = this.define(r.through, {}, {unresolved: true});
}
}
if (targetModel.settings.unresolved || (throughModel && throughModel.settings.unresolved)) {
if (!isModelDataSourceAttached(targetModel) || (throughModel && !isModelDataSourceAttached(throughModel))) {
// Create a listener to defer the relation set up
createListener(rn, r, targetModel, throughModel);
} else {