From b466bb96c80b65620e7a57d82cfa989186c9da3e Mon Sep 17 00:00:00 2001 From: Guilherme Cirne Date: Tue, 18 Feb 2014 17:40:35 -0300 Subject: [PATCH] The simplest possible solution for clearing the handler cache when registering a model. --- lib/application.js | 5 +++++ test/app.test.js | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/application.js b/lib/application.js index d1c2b17e..527f6bb1 100644 --- a/lib/application.js +++ b/lib/application.js @@ -102,6 +102,7 @@ app.model = function (Model, config) { var remotingClassName = compat.getClassNameForRemoting(Model); this.remotes().exports[remotingClassName] = Model; this.models().push(Model); + clearHandlerCache(this); Model.shared = true; Model.app = this; Model.emit('attached', this); @@ -639,6 +640,10 @@ function tryReadConfig(cwd, fileName) { } } +function clearHandlerCache(app) { + app._handlers = undefined; +} + /** * Install all express middleware required by LoopBack. * diff --git a/test/app.test.js b/test/app.test.js index 701d22fe..835dcbdc 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -29,6 +29,14 @@ describe('app', function() { expect(app.remotes().exports).to.eql({ color: Color }); }); + it('clears handler cache', function() { + var originalRestHandler = app.handler('rest'); + var Color = db.createModel('color', {name: String}); + app.model(Color); + var newRestHandler = app.handler('rest'); + expect(originalRestHandler).to.not.equal(newRestHandler); + }); + describe('in compat mode', function() { before(function() { loopback.compat.usePluralNamesForRemoting = true; @@ -89,7 +97,7 @@ describe('app', function() { beforeEach(function () { app.boot({ app: { - port: 3000, + port: 3000, host: '127.0.0.1', restApiRoot: '/rest-api', foo: {bar: 'bat'}, @@ -106,7 +114,7 @@ describe('app', function() { dataSources: { 'the-db': { connector: 'memory' - } + } } }); }); @@ -153,14 +161,14 @@ describe('app', function() { var app = loopback(); app.boot({ app: { - port: undefined, + port: undefined, host: undefined } }); return app; } }); - + it('should be honored', function() { var assertHonored = function (portKey, hostKey) { process.env[hostKey] = randomPort();