Permalink
Browse files

Merge pull request #9 from zipang/master

Setting up the context of controllers' calls
  • Loading branch information...
2 parents 37b9df8 + 5f9707f commit bc72e8aa2bf6033f03911524bef4f39c451dfd7d @chetan51 committed Jul 19, 2012
Showing with 9 additions and 7 deletions.
  1. +9 −7 lib/ni.js
View
@@ -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();
+module.exports = new Ni();

0 comments on commit bc72e8a

Please sign in to comment.