Permalink
Browse files

Examples was broken, fixed. Minor refactoring.

  • Loading branch information...
1 parent e3bcc38 commit ee03858a38e768f8cc0b8b481cd655a3e4bf5210 @kishorenc committed Dec 11, 2011
Showing with 52 additions and 58 deletions.
  1. +1 −1 example/app.js
  2. +51 −57 lib/road.js
View
2 example/app.js
@@ -10,7 +10,7 @@ var road = require('road');
// configure road by specifying:
// view engine, application root, routes (see routes.js), and whether layout should be used
-road.configure('ejs', __dirname, require('./routes'), false);
+road.configure('ejs', __dirname, require('./routes'));
app.configure(function() {
app.use(express.static(__dirname+'/public'));
View
108 lib/road.js
@@ -3,77 +3,71 @@ var fs = require('fs');
var config = {};
this.configure = function (viewExtension, appRoot, routes, useLayout) {
- config.viewExtension = viewExtension;
- config.appRoot = appRoot;
- config.routes = routes || [];
- config.useLayout = useLayout || true;
+ config.viewExtension = viewExtension;
+ config.appRoot = appRoot;
+ config.routes = routes || [];
+ config.useLayout = (useLayout === undefined) ? false : useLayout;
}
this.router = function (app) {
- for (var index in config.routes) {
- var route = config.routes[index];
- if (route[0] == 'map') {
- // custom routes
- var method = route[1],
- path = route[2],
- controllerName = route[3],
- actionName = route[4];
- app[method].apply(null, [path, mappedResponse(controllerName, actionName)]);
- } else {
- // default routes
- var method = route[0],
- path = route[1];
- app[method].apply(null, [path, function (req, res) {
- var controller = req.params.controller || "index";
- var action = req.params.action || "index";
- callController(controller, action, req, res);
- }]);
- }
+ for (var index in config.routes) {
+ var route = config.routes[index];
+ if (route[0] == 'map') {
+ var method = route[1],
+ path = route[2],
+ controllerName = route[3],
+ actionName = route[4];
+ app[method](path, function (req, res) {
+ callController(controllerName, actionName)
+ });
+ } else {
+ var method = route[0],
+ path = route[1];
+ app[method](path, function (req, res) {
+ var controller = req.params.controller || "index";
+ var action = req.params.action || "index";
+ callController(controller, action, req, res);
+ });
}
+ }
};
-function mappedResponse(controllerName, actionName) {
- return function (req, res) {
- callController(controllerName, actionName, req, res);
- };
-}
-
function callController(controllerName, functionName, req, res) {
- var controller = require(config.appRoot+'/controllers/' + controllerName + "Controller");
- var connection = {
- 'req': req,
- 'res': res
- };
- controller[functionName].apply(null, [connection, createControllerCallback(controllerName, req, res)]);
+ var controller = require(config.appRoot+'/controllers/' + controllerName + "Controller");
+ var connection = {
+ 'req': req,
+ 'res': res
+ };
+ controller[functionName].apply(null, [connection, createControllerCallback(controllerName, req, res)]);
}
function createControllerCallback(controllerName, req, res) {
- return function (viewReturned, contentType) {
- if (typeof (viewReturned) == 'object' && !(viewReturned instanceof Array)) {
- console.error("Road.js - exception on page:" + req.url);
- } else {
- renderView(controllerName, viewReturned, contentType, req, res);
- }
+ return function (viewReturned, contentType) {
+ if (typeof (viewReturned) == 'object' && !(viewReturned instanceof Array)) {
+ console.error("Road.js - exception on page:" + req.url);
+ } else {
+ renderView(controllerName, viewReturned, contentType, req, res);
}
+ }
}
function renderView(controllerName, viewOptions, contentType, req, res) {
- if (viewOptions.constructor === Array) {
- // viewOptions = [ viewFile, [,data] ]
- var cType = contentType || 'text/html';
- res.header("Content-Type", cType);
+ if (viewOptions.constructor === Array) {
+ // viewOptions : [ viewFile, [,data] ]
+ var cType = contentType || 'text/html';
+ res.header("Content-Type", cType);
- if (!viewOptions[1]) viewOptions[1] = {};
- viewOptions[1].layout = config.useLayout;
+ if (!viewOptions[1]) viewOptions[1] = {};
+ viewOptions[1].layout = config.useLayout;
- var viewFile = controllerName + '/' + viewOptions[0] + '.' + config.viewExtension;
- res.render(viewFile, viewOptions[1]);
- } else {
- // plain string passed
- var cType = contentType || 'text/plain';
- res.writeHead(200, {
- 'Content-Type': cType
- });
- res.end(viewOptions);
- }
+ var viewFile = controllerName + '/' + viewOptions[0] + '.' + config.viewExtension;
+ res.render(viewFile, viewOptions[1]);
+ } else {
+ // plain string passed
+ var cType = contentType || 'text/plain';
+ res.writeHead(200, {
+ 'Content-Type': cType
+ });
+ res.end(viewOptions);
+ }
}

0 comments on commit ee03858

Please sign in to comment.