Skip to content

Commit

Permalink
Merge pull request #9 from zipang/master
Browse files Browse the repository at this point in the history
Setting up the context of controllers' calls
  • Loading branch information
chetan51 committed Jul 19, 2012
2 parents 37b9df8 + 5f9707f commit bc72e8a
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;

/*
* 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.
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -491,4 +493,4 @@ function loadFilesFromDir(dir, fnOnFile, callback) {
* Exports the Ni object to Node.js.
*/

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

0 comments on commit bc72e8a

Please sign in to comment.