Sails version: v0.12.11
Node version: v6.3.0
NPM version: v3.10.8
Operating system: Win10
Pretty straight forward: shouldn't this be possible?
// api/controller/TestController.js
class TestController {
index(req, res) {
res.send('Hello World.');
}
}
module.exports = new TestController();
// config/routes.js
'/': 'TestController.index'
Error:
error: Ignored attempt to bind route (/) to unknown controller.action :: test.index
Hey @nehalist, thanks for pointing this out. We should make the code that loads controllers/actions a bit stricter. i.e. in this case, I would expect Sails to refuse to lift, and see something like:
Cannot register controller (`TestController`) because it is not defined as a dictionary (plain JavaScript object). Instead, got: .....
Wouldn't it be useful to actually support class usage instead of changing the error message? Or are there any arguments against?
+1
@mikermcneil I wonder why this did not work, how is it that assigning an object literal to module.exports works but class instance doesn't. They are essentially same - Objects
If 1.0 doesn't support class usage at all, the best workaround would probably be to use Babel. Since v1.0 brings language support to "userland" (e.g. http://sailsjs.com/documentation/tutorials/using-coffee-script) this should also be possible with babel (babel/register).
Edit: Tried that today. Does __not__ work.
@nehalist,@mikermcneil,@xhallix,@adnan-kamili: Hello, I'm a repo bot-- nice to meet you!
It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message and simply close the issue if possible. On the other hand, if you are still waiting on a patch, please post a comment to keep the thread alive (with any new information you can provide).
If no further activity occurs on this thread within the next 3 days, the issue will automatically be closed.
Thanks so much for your help!
Hi @nehalist,@mikermcneil,@xhallix,@adnan-kamili;
Just to loop back around on this, the reason that ES6 class instances don't work as Sails controllers is precisely because they are _not_ plain objects. Sails expects controllers to be dictionaries whose keys (the action names) can be enumerated using Object.keys() (or to be more specific, the Lodash equivalent). This has some internal benefits when it comes to validating actions, as well as supporting the _config key on controllers.
It's not clear to me what benefits you would get from declaring a Sails controller as a single instance of a class. Your controllers should be stateless, and if you're looking to use some private helper functions that are of absolutely no use to any other controllers (so that they wouldn't make good services or Sails 1.0 helpers), you can still accomplish this by declaring functions outside the module.exports block or, a bit more elegantly, by using a closure:
module.exports = (function() {
var helperFn = function() { return 'do some stuff'; }
return {
index: function(req, res) {
return res.send(helperFn());
}
}
})();
All that being said, you could still use ES6 functions in your controller file, but you'll have to add the actions individually to the module.exports, either using a self-invoking function like above, or something like:
// api/controller/TestController.js
class TestController {
index(req, res) {
res.send('Hello World.');
}
}
var controller = new TestController();
module.exports = {
index: controller.index
}
Most helpful comment
Wouldn't it be useful to actually support
classusage instead of changing the error message? Or are there any arguments against?