From 5f9707f669de44c322cce0646535374e9b851669 Mon Sep 17 00:00:00 2001 From: Christophe Desguez Date: Wed, 18 Jul 2012 17:42:53 +0200 Subject: [PATCH] Set the optional call context for controllers. --- lib/ni.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/ni.js b/lib/ni.js index c979c6e..4ffa324 100644 --- a/lib/ni.js +++ b/lib/ni.js @@ -23,10 +23,12 @@ var Ni = function() { var Ni = this; /* - * Registry for Ni and custom configuration variables. + * Set the optional call context for controllers (should be the running app). */ - this.config = {}; + this.setContext = function(ctx) { + this.context = ctx; + }; /* * Getter and setter for configuration variables. @@ -144,11 +146,11 @@ var Ni = function() { if (typeof(controller.__init) === 'function' && fn) { args.unshift(function () { args.shift(); - fn.apply(null, args); + fn.apply(Ni.context, args); }); controller.__init.apply(null, args); } else if (fn) { - fn.apply(null, args); + fn.apply(Ni.context, args); } else { res.Ni.controller = undefined; @@ -168,10 +170,10 @@ var Ni = function() { if (typeof(controller.__init) === 'function') { controller.__init(function () { - controller.index(req, res, next); + controller.index.call(Ni.context, req, res, next); }, req, res, next); } else { - controller.index(req, res, next); + controller.index.call(Ni.context, req, res, next); } } else { @@ -491,4 +493,4 @@ function loadFilesFromDir(dir, fnOnFile, callback) { * Exports the Ni object to Node.js. */ -module.exports = new Ni(); \ No newline at end of file +module.exports = new Ni();