diff --git a/lib/datasource.js b/lib/datasource.js index 70040a28..5ff6711b 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -548,26 +548,23 @@ DataSource.prototype.getModelDefinition = function(name) { DataSource.prototype.attach = function (modelClass) { if(modelClass.dataSource === this) { // Already attached to the data source - return; + return modelClass; + } + + if(modelClass.modelBuilder !== this.modelBuilder) { + this.modelBuilder.definitions[modelClass.modelName] = modelClass.definition; + this.modelBuilder.models[modelClass.modelName] = modelClass; + // reset the modelBuilder + modelClass.modelBuilder = this.modelBuilder; } - var className = modelClass.modelName; - var modelDef = modelClass.modelBuilder.getModelDefinition(className); - var properties = modelDef.properties; - var settings = modelDef.settings; // redefine the dataSource modelClass.dataSource = this; - // add to def - var def = new ModelDefinition(this.modelBuilder, className, properties, settings); - def.build(); - this.modelBuilder.definitions[className] = def; - this.modelBuilder.models[className] = modelClass; - - this.setupDataAccess(modelClass, settings); + this.setupDataAccess(modelClass, modelClass.settings); modelClass.emit('dataSourceAttached', modelClass); + return modelClass; - return this; }; /**