Last active
September 20, 2018 05:53
-
-
Save pulkitsinghal/3eeaec87d536a822ee0c to your computer and use it in GitHub Desktop.
Non-Static (instance) remote methods for loopback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loopback = require('loopback'); | |
// HINT(s): | |
// Getting the app object: http://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects | |
// From a model script: http://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects#WorkingwithLoopBackobjects-Fromamodelscript | |
module.exports = function(StoreModel) { | |
StoreModel.prototype.instanceRemoteMethodSignature = function(cb) { | |
console.log('print this instance object: ', this); | |
cb(null); | |
}; | |
StoreModel.remoteMethod( | |
'__get__instance-remote-method-signature', | |
{ | |
isStatic: false, | |
accepts: [], | |
http: {path:'/instance-remote-method-signature', verb: 'get'} | |
} | |
); | |
StoreModel.staticMethodSignature = function(id, cb) { | |
console.log('print this instance object: ', this); | |
StoreModel.findById(id,function(err, instances){ | |
if(err){ | |
cb(err); | |
} | |
console.log('print object: ', instances); | |
cb(null); | |
}); | |
}; | |
StoreModel.remoteMethod( | |
'__get__static-method-signature', | |
{ | |
isStatic: false, | |
accepts: [ | |
{arg: 'id', type: 'number', required: true} | |
], | |
http: {path:'/:id/static-method-signature', verb: 'get'} | |
} | |
); | |
}; |
I believe @nammaianh is correct the isStatic
property for the __get__static-method-signature
remote method should be set to true
. when you read the code of loopback model.js the whole purpose of the isStatic
property is to grab the instance of the model by the foreign key with the models builtin getter function. It also injects the arguments of id in the remote method accepts property. The prototype method receives the instance via the built in getter function when the remote method is set to false. Its also important to note that for every defined remote method, if isStatic
is undefined
then isStatic
is set to true
. In conclusion not only should isStatic
be set to true
but leaving it omitting the property is also a valid option.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!