Merge pull request #3322 from DAVIDFlavien/fix/nestRemotingRoutes
Add new event "remoteMethodAdded"
This commit is contained in:
commit
75fdd3cf32
|
@ -153,6 +153,9 @@ app.model = function(Model, config) {
|
||||||
Model.on('remoteMethodDisabled', function(model, methodName) {
|
Model.on('remoteMethodDisabled', function(model, methodName) {
|
||||||
self.emit('remoteMethodDisabled', model, methodName);
|
self.emit('remoteMethodDisabled', model, methodName);
|
||||||
});
|
});
|
||||||
|
Model.on('remoteMethodAdded', function(model) {
|
||||||
|
self.emit('remoteMethodAdded', model);
|
||||||
|
});
|
||||||
|
|
||||||
Model.shared = isPublic;
|
Model.shared = isPublic;
|
||||||
Model.app = this;
|
Model.app = this;
|
||||||
|
|
|
@ -468,6 +468,7 @@ module.exports = function(registry) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sharedClass.defineMethod(name, options);
|
this.sharedClass.defineMethod(name, options);
|
||||||
|
this.emit('remoteMethodAdded', this.sharedClass);
|
||||||
};
|
};
|
||||||
|
|
||||||
function setupOptionsArgs(accepts) {
|
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
|
if (options.hooks === false) return; // don't inherit before/after hooks
|
||||||
|
|
||||||
self.once('mounted', function(app, sc, remotes) {
|
self.once('mounted', function(app, sc, remotes) {
|
||||||
|
|
|
@ -686,6 +686,33 @@ describe('app', function() {
|
||||||
expect(disabledRemoteMethod).to.eql('findOne');
|
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) {
|
it.onServer('updates REST API when a new model is added', function(done) {
|
||||||
app.use(loopback.rest());
|
app.use(loopback.rest());
|
||||||
request(app).get('/colors').expect(404, function(err, res) {
|
request(app).get('/colors').expect(404, function(err, res) {
|
||||||
|
|
|
@ -852,6 +852,42 @@ describe.onServer('Remote Methods', function() {
|
||||||
|
|
||||||
expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne');
|
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() {
|
describe('Model.getApp(cb)', function() {
|
||||||
|
|
Loading…
Reference in New Issue