Skip to content

Commit

Permalink
Move context to this. Closes #1202
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 11, 2013
1 parent 4ef086a commit 76e33b1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
37 changes: 18 additions & 19 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ The following options are available when adding a route:
- `config` - additional route configuration (the `config` options allows splitting the route information from its implementation):
- `handler` - an alternative location for the route handler function. Same as the `handler` option in the parent level. Can only
include one handler per route.
- `context` - any value passed back to the provided handler (via the `reply.context` variable) when called. Can only be used with
`handler` function values.
- `context` - an object passed back to the provided handler (via `this`) when called. Can only be used with `handler` function values.

- `pre` - an array with prerequisites methods which are executed in serial or in parallel before the handler is called and are
described in [Route prerequisites](#route-prerequisites).
Expand Down Expand Up @@ -712,7 +711,7 @@ last path segment).
```javascript
var getPerson = function (request, reply) {

var nameParts = this.params.name.split('/');
var nameParts = request.params.name.split('/');
reply({ first: nameParts[0], last: nameParts[1] });
};

Expand Down Expand Up @@ -1403,25 +1402,25 @@ Registers an extension function in one of the available [extension points](#requ
- `event` - the event name.
- `method` - a function or an array of functions to be executed at a specified point during request processing. The required extension function signature
is `function(request, next, context)` where:
is `function(request, next)` where:
- `request` - the incoming `request` object.
- `next` - the callback function the extension method must call to return control over to the router with signature `function(exit)` where:
- `exit` - optional request processing exit response. If set to a non-falsy value, the request lifecycle process will jump to the
"send response" step, skipping all other steps in between, and using the `exit` value as the new response. `exit` can be any result
value accepted by [`reply()`](#replyresult).
- `context` - the context object provided via `options.context`.
- `this` - the context object provided via `options.context`.
- `options` - an optional object with the following:
- `before` - a string or array of strings of plugin names this method must execute before (on the same event). Otherwise, extension methods are executed
in the order added.
- `after` - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed
in the order added.
- `context` - any value passed back to the provided method (via the `context` argument) when called.
- `context` - any value passed back to the provided method (via `this`) when called.

```javascript
var Hapi = require('hapi');
var server = new Hapi.Server();

server.ext('onRequest', function (request, next, context) {
server.ext('onRequest', function (request, next) {

// Change all requests to '/test'
request.setUrl('/test');
Expand Down Expand Up @@ -1721,7 +1720,7 @@ _Available only in `'onRequest'` extension methods._
var Hapi = require('hapi');
var server = new Hapi.Server();

server.ext('onRequest', function (request, next, context) {
server.ext('onRequest', function (request, next) {

// Change all requests to '/test'
request.setUrl('/test');
Expand All @@ -1741,7 +1740,7 @@ Changes the request method before the router begins processing the request where
var Hapi = require('hapi');
var server = new Hapi.Server();

server.ext('onRequest', function (request, next, context) {
server.ext('onRequest', function (request, next) {

// Change all requests to 'GET'
request.setMethod('GET');
Expand Down Expand Up @@ -1773,9 +1772,9 @@ server.on('request', function (request, event, tags) {
}
});

var handler = function () {
var handler = function (request, reply) {

this.log(['test', 'error'], 'Test event');
request.log(['test', 'error'], 'Test event');
};
```
Expand Down Expand Up @@ -1875,7 +1874,7 @@ Useful when a view response is required outside of the handler (e.g. used in an
var Hapi = require('hapi');
var server = new Hapi.Server({ views: { engines: { html: 'handlebars' } } });

server.ext('onPreResponse', function (request, next, context) {
server.ext('onPreResponse', function (request, next) {

var response = request.response();
if (!response.isBoom) {
Expand Down Expand Up @@ -1904,7 +1903,7 @@ from within an extension point, use `next(response)` to return a different respo
var Hapi = require('hapi');
var server = new Hapi.Server();

server.ext('onPostHandler', function (request, next, context) {
server.ext('onPostHandler', function (request, next) {

var response = request.response();
if (response.variety === 'obj') {
Expand Down Expand Up @@ -2104,7 +2103,7 @@ var handler2 = function (request, reply) {
var handler3 = function (request, reply) {

// Echo back request stream
reply(this.raw.req).bytes(this.raw.req.headers['content-length']);
reply(request.raw.req).bytes(request.raw.req.headers['content-length']);
};
```
Expand All @@ -2125,7 +2124,7 @@ JavaScript object, sent stringified. The 'Content-Type' header defaults to 'appl
var Hapi = require('hapi');
var server = new Hapi.Server();

server.ext('onPostHandler', function (request, next, context) {
server.ext('onPostHandler', function (request, next) {

var response = request.response();
if (response.variety === 'obj') {
Expand Down Expand Up @@ -2388,7 +2387,7 @@ response object.
var Hapi = require('hapi');
var server = new Hapi.Server({ views: { engines: { html: 'handlebars' } } });

server.ext('onPreResponse', function (request, next, context) {
server.ext('onPreResponse', function (request, next) {

var response = request.response();
if (!response.isBoom) {
Expand Down Expand Up @@ -3265,12 +3264,12 @@ exports.register = function (plugin, options, next) {
#### `plugin.context(context)`
Sets a global plugin context used as the default context when adding a route or an extension using the plugin interface (if no
explicit context is provided as an option).
explicit context is provided as an option). The context object is made available within the handler and extension methods via `this`.
```javascript
var handler = function (request, reply) {
this.reply(reply.context.message);
request.reply(this.message);
};
exports.register = function (plugin, options, next) {
Expand Down Expand Up @@ -3433,7 +3432,7 @@ Adds an extension point method to the selected pack's servers as described in [`
```javascript
exports.register = function (plugin, options, next) {
plugin.ext('onRequest', function (request, extNext, context) {
plugin.ext('onRequest', function (request, extNext) {
console.log('Received request: ' + request.path);
extNext();
Expand Down
5 changes: 2 additions & 3 deletions lib/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internals.Ext.prototype.invoke = function (request, event, callback) {

Async.forEachSeries(handlers, function (ext, next) {

var context = (request && ext.env ? ext.env.context : undefined);
var context = (request && ext.env ? ext.env.context : null);
if (request) {
request._pluginEnv = ext.env;
}
Expand All @@ -91,14 +91,13 @@ internals.Ext.prototype.invoke = function (request, event, callback) {

enter(function () {

ext.func(request || ext.env.root, exit, context);
ext.func.call(context, request || ext.env.root, exit);
});
});
},
function (err) {

if (request) {
request.context = undefined;
request._pluginEnv = undefined;
}

Expand Down
11 changes: 6 additions & 5 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ internals.Handler.prototype.after = function (callback, err) {
// Decorate request

var reply = internals.decorateReply(request, finalize);
var context = (request.route.context || request._route.env.context);

// Execute handler

enter(function () {

request.server._dtrace.report('request.handler', request);
request.route.handler.call(null, request, reply);
request.route.handler.call(context, request, reply);
});
});
};
Expand All @@ -156,8 +157,6 @@ internals.decorateReply = function (request, finalize) {
return Response._generate(result, request, finalize);
};

reply.context = (request.route.context || request._route.env.context);

reply.close = function () {

finalize(new Closed());
Expand All @@ -179,7 +178,7 @@ internals.decorateReply = function (request, finalize) {
reply.proxy = function (options) {

var handler = Proxy.handler(options, request._route);
handler.call(null, request, reply);
handler.call(null, request, reply); // Internal handler not using context
};

return reply;
Expand Down Expand Up @@ -307,10 +306,12 @@ internals.bindPre = function (pre) {
return exit();
};

var context = (request.route.context || request._route.env.context);
var reply = internals.decorateReply(request, finalize);

enter(function () {

request.server._dtrace.report('pre.start', pre.assign);
var reply = internals.decorateReply(request, finalize);
pre.method.call(null, request, reply);
});
});
Expand Down
1 change: 1 addition & 0 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ internals.Pack.prototype._register = function (plugin, permissions, options, cal

root.context = function (context) {

Utils.assert(typeof context === 'object', 'context must be an object');
env.context = context;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ internals.routeConfigSchema = {
view: Joi.string().required()
})
],
context: Joi.object(),
context: Joi.object().allow(null),
payload: [Joi.string().valid('stream', 'raw', 'parse', 'try'),
Joi.object({
mode: Joi.string().valid(['stream', 'raw', 'parse', 'try']),
Expand Down
6 changes: 3 additions & 3 deletions test/integration/pack/--context/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ exports.register = function (plugin, options, next) {

internals.handler = function (request, reply) {

reply(reply.context.value);
reply(this.value);
};


internals.ext = function (request, next, context) {
internals.ext = function (request, next) {

request.response()._payload.push(context.suffix);
request.response()._payload.push(this.suffix);
next();
};

0 comments on commit 76e33b1

Please sign in to comment.