From af26e098459a1b338d023593fa826aeebbc7c142 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 24 Jul 2014 17:00:27 -0700 Subject: [PATCH] Emit a 'modelRemoted' event by app.model() This event will be listened by loopback-explorer so that models remoted after the explorer is initiated will be added to the api specs --- lib/application.js | 4 +++- lib/browser-express.js | 5 +++++ test/app.test.js | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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) {