diff --git a/lib/application.js b/lib/application.js index 84884098..9943f0a9 100644 --- a/lib/application.js +++ b/lib/application.js @@ -152,9 +152,11 @@ app.model = function (Model, config) { if (isPublic && Model.sharedClass) { this.remotes().addClass(Model.sharedClass); - if (Model.settings.trackChanges && Model.Change) + if (Model.settings.trackChanges && Model.Change) { this.remotes().addClass(Model.Change.sharedClass); + } clearHandlerCache(this); + this.emit('modelRemoted', Model.sharedClass); } Model.shared = isPublic; diff --git a/lib/browser-express.js b/lib/browser-express.js index 82aba2fa..2a7dbe91 100644 --- a/lib/browser-express.js +++ b/lib/browser-express.js @@ -1,3 +1,6 @@ +var EventEmitter = require('events').EventEmitter; +var util = require('util'); + module.exports = browserExpress; function browserExpress() { @@ -10,6 +13,8 @@ function BrowserExpress() { this.settings = {}; } +util.inherits(BrowserExpress, EventEmitter); + BrowserExpress.prototype.set = function(key, value) { if (arguments.length == 1) { return this.get(key); diff --git a/test/app.test.js b/test/app.test.js index 8bed1cb2..699f175d 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -47,6 +47,18 @@ describe('app', function() { expect(app.models.Color).to.equal(Color); }); + it("emits a `modelRemoted` event", function() { + var Color = PersistedModel.extend('color', {name: String}); + Color.shared = true; + var remotedClass; + app.on('modelRemoted', function(sharedClass) { + remotedClass = sharedClass; + }); + app.model(Color); + expect(remotedClass).to.exist; + expect(remotedClass).to.eql(Color.sharedClass); + }); + it.onServer('updates REST API when a new model is added', function(done) { app.use(loopback.rest()); request(app).get('/colors').expect(404, function(err, res) {