Skip to content

Commit

Permalink
Call new disable remote method from model class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pringle committed Sep 21, 2016
1 parent 92ed213 commit 0ab33a8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
19 changes: 18 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var SharedClass = require('strong-remoting').SharedClass;
var extend = require('util')._extend;
var format = require('util').format;

var deprecated = require('depd')('loopback');

module.exports = function(registry) {
/**
* The base class for **all models**.
Expand Down Expand Up @@ -436,7 +438,22 @@ module.exports = function(registry) {
*/

Model.disableRemoteMethod = function(name, isStatic) {
this.sharedClass.disableMethod(name, isStatic || false);
deprecated('Model.disableRemoteMethod is deprecated. ' +
'Use Model.disableRemoteMethodByName instead.');
var key = this.sharedClass.getKeyFromMethodNameAndTarget(name, isStatic);
this.sharedClass.disableMethodByName(key);
this.emit('remoteMethodDisabled', this.sharedClass, key);
};

/**
* Disable remote invocation for the method with the given name.
*
* @param {String} name The name of the method (include "prototype." if the method is defined on the prototype).
*
*/

Model.disableRemoteMethodByName = function(name) {
this.sharedClass.disableMethodByName(name);
this.emit('remoteMethodDisabled', this.sharedClass, name);
};

Expand Down
2 changes: 1 addition & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ describe('app', function() {
disabledRemoteMethod = methodName;
});
app.model(Color);
app.models.Color.disableRemoteMethod('findOne');
app.models.Color.disableRemoteMethodByName('findOne');
expect(remoteMethodDisabledClass).to.exist;
expect(remoteMethodDisabledClass).to.eql(Color.sharedClass);
expect(disabledRemoteMethod).to.exist;
Expand Down
16 changes: 15 additions & 1 deletion test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,21 @@ describe.onServer('Remote Methods', function() {
var callbackSpy = require('sinon').spy();
var TestModel = app.models.TestModelForDisablingRemoteMethod;
TestModel.on('remoteMethodDisabled', callbackSpy);
TestModel.disableRemoteMethod('findOne');
TestModel.disableRemoteMethod('findOne', true);

expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne');
});

it('emits a `remoteMethodDisabled` event from disableRemoteMethodByName', function() {
var app = loopback();
var model = PersistedModel.extend('TestModelForDisablingRemoteMethod');
app.dataSource('db', { connector: 'memory' });
app.model(model, { dataSource: 'db' });

var callbackSpy = require('sinon').spy();
var TestModel = app.models.TestModelForDisablingRemoteMethod;
TestModel.on('remoteMethodDisabled', callbackSpy);
TestModel.disableRemoteMethodByName('findOne');

expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne');
});
Expand Down

0 comments on commit 0ab33a8

Please sign in to comment.