Register existing model to app.models during app.model()

This commit is contained in:
Raymond Feng 2014-06-09 16:31:33 -07:00
parent 6af69e322a
commit 815ad53aa4
2 changed files with 15 additions and 4 deletions

View File

@ -105,26 +105,30 @@ app.disuse = function (route) {
*/
app.model = function (Model, config) {
var modelName = Model;
if(arguments.length === 1) {
assert(typeof Model === 'function', 'app.model(Model) => Model must be a function / constructor');
assert(Model.modelName, 'Model must have a "modelName" property');
modelName = Model.modelName;
assert(modelName, 'Model must have a "modelName" property');
var remotingClassName = compat.getClassNameForRemoting(Model);
this.remotes().exports[remotingClassName] = Model;
this.models().push(Model);
this.models[modelName] =
this.models[classify(modelName)] =
this.models[camelize(modelName)] = Model;
clearHandlerCache(this);
Model.shared = true;
Model.app = this;
Model.emit('attached', this);
return;
}
var modelName = Model;
config = config || {};
assert(typeof modelName === 'string', 'app.model(name, config) => "name" name must be a string');
Model =
Model = modelFromConfig(modelName, config, this);
this.models[modelName] =
this.models[classify(modelName)] =
this.models[camelize(modelName)] = modelFromConfig(modelName, config, this);
this.models[camelize(modelName)] = Model;
if(config.public !== false) {
this.model(Model);

View File

@ -29,6 +29,13 @@ describe('app', function() {
expect(app.remotes().exports).to.eql({ color: Color });
});
it('registers existing models to app.models', function() {
var Color = db.createModel('color', {name: String});
app.model(Color);
expect(app.models.color).to.eql(Color);
expect(app.models.Color).to.eql(Color);
});
it('updates REST API when a new model is added', function(done) {
app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) {