Permalink
Browse files

Added support for specifying a callback that road will call after vie…

…w is rendered/err encountered. Closes issue #6. Version bump.
  • Loading branch information...
1 parent 62d03b2 commit 3cc0712a442213cfed03852d2bcaf85df4af25c9 @kishorenc committed Dec 23, 2011
Showing with 41 additions and 18 deletions.
  1. +10 −1 README.md
  2. +11 −8 example/app.js
  3. +19 −8 lib/road.js
  4. +1 −1 package.json
View
11 README.md
@@ -1,6 +1,6 @@
#Road - route helper for Express
-A route helper for express that allows you to map routes, controllers and views by following logical conventions. See example application for use cases. You need express and ejs available to run the example.
+A route helper for express that allows you to map routes, controllers and views by following logical conventions. See example application for use cases. You need express 2.x and ejs available to run the example.
##Installation
@@ -94,6 +94,15 @@ Road also supports custom routes.
];
+Finally, if you want to register a callback for Road to call when it's done rendering the view (or encounters an error), you can do so like this:
+
+ // Configure road:
+ // view engine, routes (see routes.js), useLayout?, callback
+ road.configure('ejs', require('./routes'), false, function(err, next) {
+ if(err) return next(err);
+ // do something ...
+ });
+
##Todo
* Refer to issues
View
19 example/app.js
@@ -3,23 +3,26 @@
* Make sure you have express, ejs, and (of course) road installed to run the example
*/
-var express = require('express'),
+var express = require('express'),
+ road = require('road');
app = express.createServer();
-var road = require('road');
-
-// configure road by specifying:
-// view engine, routes (see routes.js), and whether layout should be used
-road.configure('ejs', require('./routes'), false);
-
app.configure(function() {
app.use(express.static(__dirname+'/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
-
app.use(app.router);
});
+/* Configure road:
+* view engine, routes (see routes.js), useLayout?, callback
+*/
+
+road.configure('ejs', require('./routes'), false, function(err, next) {
+ if(err) return next(err);
+ // do something ...
+});
+
// use road to handle the routes this way:
road.router(app);
View
27 lib/road.js
@@ -2,13 +2,14 @@ var fs = require('fs');
var config = {};
-exports.version = '0.1.2';
+exports.version = '0.2.0';
-this.configure = function (viewExtension, routes, useLayout) {
+this.configure = function (viewExtension, routes, useLayout, callback) {
config.viewExtension = viewExtension;
config.appRoot = process.cwd();
config.routes = routes || [];
config.useLayout = (useLayout === undefined) ? false : useLayout;
+ config.callback = callback || null;
config.routes.push(['all', '/:controller?/:action?/:id?']);
}
@@ -21,7 +22,7 @@ this.router = function (app) {
controllerName = route[3],
actionName = route[4];
app[method](path, function (req, res, next) {
- callController(controllerName, actionName, req, res)
+ callController(controllerName, actionName, req, res, next)
});
} else {
var method = route[0],
@@ -36,14 +37,18 @@ this.router = function (app) {
};
function callController(controllerName, functionName, req, res, next) {
- var controller = require(config.appRoot+'/controllers/' + controllerName + "Controller");
- var controllerMethod = req.method.toLowerCase()+'_'+functionName;
- controller[controllerMethod](req, res, controllerCallback(controllerName, req, res, next));
+ try {
+ var controller = require(config.appRoot+'/controllers/' + controllerName + "Controller");
+ var controllerMethod = req.method.toLowerCase()+'_'+functionName;
+ controller[controllerMethod](req, res, controllerCallback(controllerName, req, res, next));
+ } catch(err) {
+ handleError(err, next);
+ }
}
function controllerCallback(controllerName, req, res, next) {
return function (err, view, viewData, contentType) {
- if (err) return next(err);
+ if (err) return handleError(err, next);
if(typeof(view) == 'object' && !(view instanceof Array)) {
for (var cType in view) break;
@@ -62,7 +67,8 @@ function controllerCallback(controllerName, req, res, next) {
}
renderRichView(controllerName, view, viewData, contentType, req, res);
} else {
- next(new Error("Road - could not parse your view rendering options."));
+ var parseError = new Error("Road - could not parse your view rendering options.");
+ return handleError(parseError, next);
}
}
}
@@ -79,4 +85,9 @@ function renderRichView(controllerName, view, viewData, contentType, req, res) {
var viewFile = controllerName + '/' + view + '.' + config.viewExtension;
viewData.layout = config.useLayout;
res.render(viewFile, viewData);
+}
+
+function handleError(err, next) {
+ if(config.callback) return config.callback(err, next);
+ else return next(err);
}
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "road",
- "version": "0.1.2",
+ "version": "0.2.0",
"description": "A route helper for express.",
"homepage": "https://github.com/kishorenc/road",
"author": "Kishore Nallan <kishore@kishorelive.com>",

0 comments on commit 3cc0712

Please sign in to comment.