Skip to content

Commit

Permalink
Set the optional call context for controllers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zipang committed Jul 18, 2012
1 parent 7daa1a2 commit 5f9707f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/ni.js
Expand Up @@ -23,10 +23,12 @@ var Ni = function() {
var Ni = this; 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. * Getter and setter for configuration variables.
Expand Down Expand Up @@ -144,11 +146,11 @@ var Ni = function() {
if (typeof(controller.__init) === 'function' && fn) { if (typeof(controller.__init) === 'function' && fn) {
args.unshift(function () { args.unshift(function () {
args.shift(); args.shift();
fn.apply(null, args); fn.apply(Ni.context, args);
}); });
controller.__init.apply(null, args); controller.__init.apply(null, args);
} else if (fn) { } else if (fn) {
fn.apply(null, args); fn.apply(Ni.context, args);
} }
else { else {
res.Ni.controller = undefined; res.Ni.controller = undefined;
Expand All @@ -168,10 +170,10 @@ var Ni = function() {


if (typeof(controller.__init) === 'function') { if (typeof(controller.__init) === 'function') {
controller.__init(function () { controller.__init(function () {
controller.index(req, res, next); controller.index.call(Ni.context, req, res, next);
}, req, res, next); }, req, res, next);
} else { } else {
controller.index(req, res, next); controller.index.call(Ni.context, req, res, next);
} }
} }
else { else {
Expand Down Expand Up @@ -491,4 +493,4 @@ function loadFilesFromDir(dir, fnOnFile, callback) {
* Exports the Ni object to Node.js. * Exports the Ni object to Node.js.
*/ */


module.exports = new Ni(); module.exports = new Ni();

0 comments on commit 5f9707f

Please sign in to comment.