From 981bd309fc087f36f48bbb989ee2aeaeb3a98285 Mon Sep 17 00:00:00 2001 From: Flavien DAVID Date: Wed, 29 Mar 2017 17:25:56 +0200 Subject: [PATCH] Add new event "remoteMethodAdded" Emit a new method "remoteMethodAdded" whenever a new method is added, typically when `Model.remoteMethod` or `Model.nestRemoting` is called. The method is emitted both by the Model affected and the application object. --- lib/application.js | 3 +++ lib/model.js | 3 +++ test/app.test.js | 27 +++++++++++++++++++++++++++ test/model.test.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/lib/application.js b/lib/application.js index 62db7858..4ca66653 100644 --- a/lib/application.js +++ b/lib/application.js @@ -153,6 +153,9 @@ app.model = function(Model, config) { Model.on('remoteMethodDisabled', function(model, methodName) { self.emit('remoteMethodDisabled', model, methodName); }); + Model.on('remoteMethodAdded', function(model) { + self.emit('remoteMethodAdded', model); + }); Model.shared = isPublic; Model.app = this; diff --git a/lib/model.js b/lib/model.js index 9c05cf5a..f7633b4d 100644 --- a/lib/model.js +++ b/lib/model.js @@ -468,6 +468,7 @@ module.exports = function(registry) { } this.sharedClass.defineMethod(name, options); + this.emit('remoteMethodAdded', this.sharedClass); }; function setupOptionsArgs(accepts) { @@ -970,6 +971,8 @@ module.exports = function(registry) { } }); + this.emit('remoteMethodAdded', this.sharedClass); + if (options.hooks === false) return; // don't inherit before/after hooks self.once('mounted', function(app, sc, remotes) { diff --git a/test/app.test.js b/test/app.test.js index ab56eff3..6bd234bd 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -686,6 +686,33 @@ describe('app', function() { expect(disabledRemoteMethod).to.eql('findOne'); }); + it('emits a `remoteMethodAdded` event', function() { + app.dataSource('db', {connector: 'memory'}); + var Book = app.registry.createModel( + 'Book', + {name: 'string'}, + {plural: 'books'} + ); + app.model(Book, {dataSource: 'db'}); + + var Page = app.registry.createModel( + 'Page', + {name: 'string'}, + {plural: 'pages'} + ); + app.model(Page, {dataSource: 'db'}); + + Book.hasMany(Page); + + var remoteMethodAddedClass; + app.on('remoteMethodAdded', function(sharedClass) { + remoteMethodAddedClass = sharedClass; + }); + Book.nestRemoting('pages'); + expect(remoteMethodAddedClass).to.exist(); + expect(remoteMethodAddedClass).to.eql(Book.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) { diff --git a/test/model.test.js b/test/model.test.js index 9b7b3235..50d62127 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -852,6 +852,42 @@ describe.onServer('Remote Methods', function() { expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne'); }); + + it('emits a `remoteMethodAdded` event', function() { + var app = loopback(); + app.dataSource('db', {connector: 'memory'}); + + var User = app.registry.getModel('User'); + app.model(User, {dataSource: 'db'}); + + var Token = app.registry.getModel('AccessToken'); + app.model(Token, {dataSource: 'db'}); + + var callbackSpy = require('sinon').spy(); + var TestModel = app.models.User; + TestModel.on('remoteMethodAdded', callbackSpy); + TestModel.nestRemoting('accessTokens'); + + expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass); + }); + }); + + it('emits a `remoteMethodAdded` event from remoteMethod', function() { + var app = loopback(); + var model = PersistedModel.extend('TestModelForAddingRemoteMethod'); + app.dataSource('db', {connector: 'memory'}); + app.model(model, {dataSource: 'db'}); + + var callbackSpy = require('sinon').spy(); + var TestModel = app.models.TestModelForAddingRemoteMethod; + TestModel.on('remoteMethodAdded', callbackSpy); + TestModel.remoteMethod('getTest', { + accepts: {arg: 'options', type: 'object', http: 'optionsFromRequest'}, + returns: {arg: 'test', type: 'object'}, + http: {verb: 'GET', path: '/test'}, + }); + + expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass); }); describe('Model.getApp(cb)', function() {