Open ktkaushik opened 10 years ago
See this test to get idea of how to deal with class-based controller: https://github.com/1602/kontroller/blob/master/test/base.test.js#L62
And this test case for eval controller: https://github.com/1602/kontroller/blob/master/test/base.test.js#L30
Now you are trying to use eval controller, but I would recommend you to write class-based controller (noeval).
Nope, i am not really getting an idea of how the class-based controller's been written. Could you point me to an example or something please ?
The better question here is... how do we implement the before and possibly an after middleware/filter using the class based controller?
It says in the docs that you will be getting rid of the eval style so I'm assuming you have something planned or already in place that I'm missing.
????? thanks!
It's easy:
function UsersController(init) { init.before(function requireUser(c) { if (c.req.user) c.next(); else c.next(new Error('User required')); }, {except: 'index'}); init.after(function logData(c) { doSomethingAfterProcessingRequest(); c.next(); }); }
On Thu, Oct 3, 2013 at 8:41 PM, Mike Puerto notifications@github.comwrote:
The better question here is... how do we implement the before and possibly an after middleware/filter using the class based controller?
It says in the docs that you will be getting rid of the eval style so I'm assuming you have something planned or already in place that I'm missing.
????? thanks!
— Reply to this email directly or view it on GitHubhttps://github.com/1602/kontroller/issues/11#issuecomment-25651526 .
What instantiates UsersController? And with that said, what is init and what is it an instance of?
Any info on the above?
In case of any doubts you can refer to compoundjs code uses Kontroller: https://github.com/1602/compound/blob/master/lib/controller-bridge.js#L226
Or as described in readme, you can call constructClass(name, ControllerClass)
. For example:
function MyController(init) { init.before(someBeforeHook); }
require('kontroller').BaseController.constructClass('MyController', MyController);
that will instantiate MyController and populate actions/hooks and other settings of controller. Argument init
passed to controller refers to actual controller configurator instance used to configure everything related to controller.
I am using
kontroller
with an expressapp. Here is the controller i created named asposts_controller.js
In here, i keep getting an error saying that
before
is undefined.