Skip to content
This repository
Browse code

Merge pull request #9 from zipang/master

Setting up the context of controllers' calls
  • Loading branch information...
commit bc72e8aa2bf6033f03911524bef4f39c451dfd7d 2 parents 37b9df8 + 5f9707f
Chetan Surpur authored July 19, 2012

Showing 1 changed file with 9 additions and 7 deletions. Show diff stats Hide diff stats

  1. 16  lib/ni.js
16  lib/ni.js
@@ -23,10 +23,12 @@ var Ni = function() {
23 23
     var Ni = this;
24 24
 
25 25
     /*
26  
-     *  Registry for Ni and custom configuration variables.
  26
+     *  Set the optional call context for controllers (should be the running app).
27 27
      */
28 28
 
29  
-    this.config = {};
  29
+    this.setContext = function(ctx) {
  30
+        this.context = ctx;
  31
+    };
30 32
 
31 33
     /*
32 34
      *  Getter and setter for configuration variables.
@@ -144,11 +146,11 @@ var Ni = function() {
144 146
                 if (typeof(controller.__init) === 'function' && fn) {
145 147
                   args.unshift(function () {
146 148
                     args.shift();
147  
-                    fn.apply(null, args);
  149
+                    fn.apply(Ni.context, args);
148 150
                   });
149 151
                   controller.__init.apply(null, args);
150 152
                 } else if (fn) {
151  
-                    fn.apply(null, args);
  153
+                    fn.apply(Ni.context, args);
152 154
                 }
153 155
                 else {
154 156
                     res.Ni.controller = undefined;
@@ -168,10 +170,10 @@ var Ni = function() {
168 170
 
169 171
                 if (typeof(controller.__init) === 'function') {
170 172
                   controller.__init(function () {
171  
-                    controller.index(req, res, next);
  173
+                    controller.index.call(Ni.context, req, res, next);
172 174
                   }, req, res, next);
173 175
                 } else {
174  
-                  controller.index(req, res, next);
  176
+                  controller.index.call(Ni.context, req, res, next);
175 177
                 }
176 178
             }
177 179
             else {
@@ -491,4 +493,4 @@ function loadFilesFromDir(dir, fnOnFile, callback) {
491 493
  *  Exports the Ni object to Node.js.
492 494
  */
493 495
 
494  
-module.exports = new Ni();
  496
+module.exports = new Ni();

0 notes on commit bc72e8a

Please sign in to comment.
Something went wrong with that request. Please try again.