Search code examples
node.jscompoundjs

How do you access the built in Compoundjs helper methods from a noeval controller?


How do you access the built in Compoundjs helper methods from a noeval controller?

From an eval'd controller the helper functions seem to get loaded automatically and they are just accessible by doing something like this:

before('protectFromForgery', function () {
    protectFromForgery('some_secret_key');
});

But not sure what the best way is to access them from a non-eval'd controller.

They seem to be located in /compound/node_modules_kontroller/lib/helpers.js


Solution

  • Figured it out. All the built-in helper methods are just attached to the controller context object. (The variable 'c' in the following examples)

    So you would do something like this:

    //Example of noeval controller: app/controllers/car.js:
    
    module.exports = CarController;
    
    // load parent controller
    var Essentials = require('./essentials');
    
    function CarController(init) {
        // call parent constructor
        Essentials.call(this, init);
    
        init.before(function protectFromForgery(c) {
            c.protectFromForgery("some_secret_key");
        }, {only: 'accelerate'});
    
    }
    
    // setup inheritance
    require('util').inherits(CarController, Essentials);
    
    CarController.prototype.accelerate = function(c) {
        c.send(++this.speed);
    };
    
    CarController.prototype.brake = function(c) {
        c.send(++this.speed);
    };