From 0e72c85e0ff9c62ae2bc426da50aa82d93ce91e0 Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Mon, 29 Jun 2015 23:17:04 +0000 Subject: [PATCH 1/2] mount now allows mounting on the root --- node_modules/frontdoor/lib/section.js | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/node_modules/frontdoor/lib/section.js b/node_modules/frontdoor/lib/section.js index 778e8b92..2c03744e 100644 --- a/node_modules/frontdoor/lib/section.js +++ b/node_modules/frontdoor/lib/section.js @@ -80,8 +80,37 @@ module.exports = function Section(name, description, types) { sections[name].push(section); return section; }; + + this.getRoutes = function(){ + return routes; + }; + + /** + * Mount an existing section on this instance. + * + * Note that when the first argument is omitted, the section's routes will + * be copied onto this instance's routing table. + * + * @param {string} mountpoint Where to mount this secion, e.g. "/capture/" + * @param {Section} section A frontdoor section + */ this.mount = function(name, section) { + if ( arguments.length == 1 ){ + section = arguments[0]; + + if ( ! ( section instanceof Section ) ) + throw new Error("Single argument to mount must be a Section!"); + + var addRoutes = section.getRoutes(); + + Object.keys(addRoutes).forEach(function( method){ + routes[method] = [].concat( routes[method], addRoutes[method] ); + }); + + return; + } + if (!sections[name]) sections[name] = []; From cb07546d0b8ee390b258fdf55596f42e145daa61 Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Mon, 29 Jun 2015 23:17:28 +0000 Subject: [PATCH 2/2] adding convenient method for client-side routing --- node_modules/frontdoor/lib/section.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/node_modules/frontdoor/lib/section.js b/node_modules/frontdoor/lib/section.js index 2c03744e..bf65ef79 100644 --- a/node_modules/frontdoor/lib/section.js +++ b/node_modules/frontdoor/lib/section.js @@ -118,6 +118,25 @@ module.exports = function Section(name, description, types) { sections[name].push(section); }; + /** + * Sortcut for client-side routing without a `next` + * + * Allows you to omit the third argument, "next" and avoid special + * treatment in router.js (see "wrapHandler"). + * + * Has a variable number of argumnets: no need to wrap handlers in an array + */ + this.on = function( path, handler ){ + var middlewares = Array.prototype.slice.call( arguments, 1 ); + handler = middlewares.shift(); + + middlewares.unshift(function(req, res, next){ + handler( req, res, next ); + }); + + this._route( path, { method: "get" }, middlewares ); + }; + this._rootHandler = function(req, res) { this.handle(req, res, function(err) { if (err) {