Reputation: 1944
I have a service called MyService.js
with MyAction
exported, which can be called anywhere as
MyService.MyAction()
I need to call it as a variable. The following works
var myAction = "MyAction"
MyService[myAction]()
However, is there a way to use the same logic for the service name as well? For example:
var myAction = "MyAction"
var myService = "MyService"
myService[myAction]()
I believe this should be run using the sails
object, but I don't know where to start.
Is there anything like:
sails.services[myService][myAction]()
Upvotes: 1
Views: 44
Reputation: 1944
After fiddling with the sails object, I found a solution.
In order to call a service as variable, you need to make sure the service name is in lowercase, since how Sails store them. Apparently the actions keep their original case, so they are case sensitive.
var myAction = "MyAction"
var myService = "MyService"
sails.services[myService.strToLower()][myAction]()
Upvotes: 1