1602 / kontroller

Generic Controller for express / compound / socket.io and everything else
8 stars 5 forks source link

'before' method is undefined on expressjs #11

Open ktkaushik opened 10 years ago

ktkaushik commented 10 years ago

I am using kontroller with an expressapp. Here is the controller i created named as posts_controller.js

In here, i keep getting an error saying that before is undefined.

before(think, {only: 'accelerate'});
       ^
TypeError: Property 'before' of object function Controller() {
        if (!(this instanceof Controller)) {
            return BaseController.route(arguments[0]);
        }

        BaseController.call(this);
    } is not a function
    at Object.<anonymous> (/Users/kaushik/dev/my_repos/kontroller_sample/controllers/posts_controller.js:4:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/kaushik/dev/my_repos/kontroller_sample/app.js:11:13)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
1602 commented 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).

ktkaushik commented 10 years ago

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 ?

mikepuerto commented 10 years ago

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!

anatoliychakkaev commented 10 years ago

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 .

mikepuerto commented 10 years ago

What instantiates UsersController? And with that said, what is init and what is it an instance of?

mikepuerto commented 10 years ago

Any info on the above?

1602 commented 10 years ago

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.