From 815ad53aa4ccb99b806f158ff81b5b91c24dfb84 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 9 Jun 2014 16:31:33 -0700 Subject: [PATCH] Register existing model to app.models during app.model() --- lib/application.js | 12 ++++++++---- test/app.test.js | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/application.js b/lib/application.js index 0c373fed..0489593e 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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); diff --git a/test/app.test.js b/test/app.test.js index 74ea8923..866d2405 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -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) {