diff --git a/lib/datasource.js b/lib/datasource.js index 113f7b37..d59e2d65 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -412,9 +412,17 @@ DataSource.prototype.attach = function (ModelCtor) { // Already attached to the data source return; } - var properties = ModelCtor.dataSource.definitions[ModelCtor.modelName].properties; - var settings = ModelCtor.dataSource.definitions[ModelCtor.modelName].settings; var className = ModelCtor.modelName; + var properties = ModelCtor.dataSource.definitions[className].properties; + var settings = ModelCtor.dataSource.definitions[className].settings; + + // redefine the dataSource + ModelCtor.dataSource = this; + // add to def + var def = new ModelDefinition(this, className, properties, settings); + def.build(); + this.definitions[className] = def; + this.models[className] = ModelCtor; this.mixin(ModelCtor); @@ -426,16 +434,7 @@ DataSource.prototype.attach = function (ModelCtor) { settings: settings }); } - - // redefine the dataSource - hiddenProperty(ModelCtor, 'dataSource', this); - ModelCtor.dataSource = this; - - // add to def - this.definitions[className] = new ModelDefinition(this, className, properties, settings); - - this.models[className] = ModelCtor; - + return this; }; diff --git a/lib/model-definition.js b/lib/model-definition.js index 6fae9191..c57ad621 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -41,6 +41,7 @@ function ModelDefinition(modelBuilder, name, properties, settings) { } this.associations = []; this.properties = null; + this.build(); } util.inherits(ModelDefinition, EventEmitter);