locomotive
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -251,2 +251,48 @@ /** | ||
/** | ||
* Invoke a different controller's action in order to process the current | ||
* request. | ||
* | ||
* this.invoke('users', 'show'); | ||
* // => invokes show action of UsersController | ||
* | ||
* Shorthand notation can also be used. | ||
* | ||
* this.invoke('users#show'); | ||
* // => invokes show action of UsersController | ||
* | ||
* `controller` is optional. If not given, it will invoke a different action in | ||
* the current controller. | ||
* | ||
* this.invoke('other'); | ||
* // => invokes other action of current controller | ||
* | ||
* @param {String} controller | ||
* @param {Number} action | ||
* @api public | ||
*/ | ||
Controller.prototype.invoke = function(controller, action) { | ||
if (!action) { | ||
var split = controller.split('#'); | ||
if (split.length > 1) { | ||
// shorthand controller#action form | ||
controller = split[0]; | ||
action = split[1]; | ||
} else { | ||
action = controller; | ||
controller = this.__name; | ||
} | ||
} | ||
controller = utils.controllerize(controller); | ||
action = utils.actionize(action); | ||
// Get the middleware that the router uses to handle requests, binding it to | ||
// a different controller and action. | ||
var handle = this.__app._routes._handle(controller, action).bind(this.__app); | ||
// Forward the current request on to be handled by a different controller | ||
// and/or action. | ||
handle(this.__req, this.__res, this.__next); | ||
} | ||
/** | ||
* Finish action processing, without issuing a response. | ||
@@ -253,0 +299,0 @@ * |
@@ -14,2 +14,3 @@ /** | ||
, Controller = require('./controller') | ||
, invoke = require('./middleware/invoke') | ||
, debug = require('debug')('locomotive'); | ||
@@ -188,2 +189,28 @@ | ||
/** | ||
* Middleware that will extend `req` with an `invoke()` function. | ||
* | ||
* Once `invoke()` is exposed on a request, it can be called in order to invoke | ||
* a specific controller and action in this Locomotive appliction. This is | ||
* typically done to call into a Locmotive application from middleware or routes | ||
* that exist outside of the application itself. | ||
* | ||
* To use this middleware, place the following line in `config/environments/all.js`: | ||
* | ||
* this.use(locomotive.invoke()); | ||
* | ||
* If you are mounting multiple Locomotive applications in a single server, a | ||
* `name` option can be passed, in order to avoid collisions when extending the | ||
* request multiple times. | ||
* | ||
* this.use(locomotive.invoke({ name: 'invokeApp1' })); | ||
* | ||
* @param {Object} options | ||
* @return {Function} middleware | ||
* @api public | ||
*/ | ||
Locomotive.prototype.invoke = function(options) { | ||
return invoke(options).bind(this); | ||
} | ||
/** | ||
* Returns a string indicating the type of record of `obj`. | ||
@@ -190,0 +217,0 @@ * |
@@ -0,1 +1,3 @@ | ||
var RouterError = require('../errors').RouterError; | ||
/** | ||
@@ -2,0 +4,0 @@ * Builds a function to handle a route with given `controller` and `action`. |
@@ -10,4 +10,3 @@ /** | ||
, Namespace = require('./namespace') | ||
, Route = require('./route') | ||
, RouterError = require('./errors').RouterError; | ||
, Route = require('./route'); | ||
@@ -14,0 +13,0 @@ |
{ | ||
"name": "locomotive", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Powerful MVC web framework for Node.js.", | ||
@@ -5,0 +5,0 @@ "author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" }, |
98389
28
3029