Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed merge conflict

  • Loading branch information...
commit ea8c57e619a6a94130ed027e711c9db57dc1c10f 2 parents beb5833 + 058f2ee
@chetan51 authored
View
4 README.md
@@ -86,7 +86,7 @@ Each of those is just a Node module. For example, the calculator controller ment
* be called, so it would look like /calculator.
*/
- this.index = function(req, res) {
+ this.index = function(req, res, next) {
res.ok('Welcome to the calculator!');
}
@@ -98,7 +98,7 @@ Each of those is just a Node module. For example, the calculator controller ment
* function with a = 4 and b = 5.
*/
- this.add = function(req, res, a, b) {
+ this.add = function(req, res, next, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);
View
4 example/controllers/calculator.js
@@ -38,7 +38,7 @@ var CalculatorController = function() {
* be called, so it would look like /calculator.
*/
- this.index = function(req, res) {
+ this.index = function(req, res, next) {
res.ok('Welcome to the calculator!');
}
@@ -47,7 +47,7 @@ var CalculatorController = function() {
* to be called, so it would look like /calculator/add.
*/
- this.add = function(req, res, a, b) {
+ this.add = function(req, res, next, a, b) {
if (a && b) {
a = parseInt(a);
b = parseInt(b);
View
2  example/controllers/home.js
@@ -16,7 +16,7 @@ var Ni = require('../../lib/ni');
*/
var HomeController = function() {
- this.index = function(req, res) {
+ this.index = function(req, res, next) {
res.ok('Hello ' + Ni.config.location + '!');
}
};
View
58 lib/ni.js
@@ -28,6 +28,8 @@ var Ni = function() {
this.config = {};
+ this.automatic_views = false;
+
/*
* Tells Ni what root directory to look into.
*
@@ -71,28 +73,43 @@ var Ni = function() {
*/
this.router = function(req, res, next) {
- parsedUrl = require('url').parse(req.url, true);
- pathArr = parsedUrl.pathname.split('/');
-
- var controller;
+ var parsedUrl = require('url').parse(req.url, true),
+ pathArr = parsedUrl.pathname.split('/'),
+ args = pathArr.slice(3),
+ controller,
+ fn;
+
+ res.Ni = { view: Ni.automatic_views };
+
+ args.unshift(next);
+ args.unshift(res);
+ args.unshift(req);
if (pathArr[1]) {
controller = Ni.controllers[pathArr[1]];
if (controller) {
+ res.Ni.controller = pathArr[1];
var fn;
if (pathArr[2]) {
+ res.Ni.action = pathArr[2];
fn = controller[pathArr[2]];
}
else {
+ res.Ni.action = 'index';
fn = controller.index;
}
- if (fn) {
- var args = pathArr.slice(3);
- args.unshift(res);
- args.unshift(req);
+ if (typeof(controller.__init) === 'function' && fn) {
+ args.unshift(function () {
+ args.shift();
+ fn.apply(null, args);
+ });
+ controller.__init.apply(null, args);
+ } else if (fn) {
fn.apply(null, args);
}
else {
+ res.Ni.controller = undefined;
+ res.Ni.action = undefined;
next();
}
}
@@ -103,13 +120,36 @@ var Ni = function() {
else {
controller = Ni.controllers.home;
if (controller && controller.index) {
- controller.index(req, res);
+ res.Ni.controller = 'home';
+ res.Ni.action = 'index';
+ if (typeof(controller.__init) === 'function') {
+ controller.__init(req, res, next, {}, function () {
+ controller.index(req, res, next);
+ });
+ } else {
+ controller.index(req, res, next);
+ }
}
else {
next();
}
}
}
+
+ this.renderView = function (renderer) {
+ if (typeof(renderer) === 'function') {
+ Ni.automatic_views = true;
+ return function (req, res, next) {
+ if (req.Ni.view && typeof(req.Ni.action) !== 'undefined') {
+ renderer(req, res, next, req.Ni.controller + '/' + req.Ni.action);
+ } else {
+ next();
+ }
+ }
+ } else {
+ throw new Error('Ni.renderView expects a function as the first argument');
+ }
+ }
/*
* Gets a specific controller by name.
Please sign in to comment.
Something went wrong with that request. Please try again.