hapi-view-context

5.0.2 • Public • Published

hapi-view-context Build Status

Helpers to automate the task of loading data into view rendering contexts. Keeps your route handlers clean!

Installation

npm install hapi-view-context

Usage

hapi-view-context lets you set up data in the current view rendering context in one of four ways:

  1. pass static global-level default key:value pairs (in the plugin options context field) that will be added to all contexts:
{
  context: {
    siteVersion: '2.4.3',
    copyright: 'Copyright (c) 2016 by You'
  }
}
  1. pass a global-level context-handling function or server method (in the plugin options contextHandler field), that will be invoked whenever 'onPostHandler' is called:
{
  contextHandler: (context, request) => {
    context.id = request.id;
    return context;
  }
}
  1. set a global-level context-handling function with server.setViewContext, the context-handler will then be invoked whenever 'onPostHandler' is called:
server.setViewContext( (context, request) => {
  context.id = request.id;
  return context;
});
  1. set request-level contexts inside route handlers and in response to request events with addContext.
server.addContext(request, 'id', request.id);

Example:

// register the plugin with a hapi server:
server.register({
  register: require('hapi-view-context'),
  options: {
    // static global-level default key:value pairs in 'context' will be added to all contexts:
    context: {
      'someVariable': 'Some Value',
      'some.nested.variable': 'Another value'
    },
    // a global-level context-handling function will be called on every request:
    contextHandler: (defaultContext, request) => {
      defaultContext.fromContextMethod = 'a value from the context method';
      return defaultContext;
    }
  }
});
// a context handler that will be able to modify the context for every request
server.setViewContext( (defaultContext, request) => {
  defaultContext.fromSetViewContext = 'a value from setViewContext';
  return defaultContext;
});
// a route that renders a view:
server.route({
  method: 'POST',
  path: '/',
  handler: (request, reply) => {
    // the route handler does not need to do anything else besides call the view renderer:
    reply.view('myView');
  }
});

Assuming that 'myView' is an HTML view that looks something like:

<h1> {{someVariable}}, {{some.nested.variable}}, {{fromContextMethod}}, {{fromSetViewContext}}.
</h1>

Then posting to the '/' route will render myView as the following:

<h1> Some Value, Another value, a value from the context method, a value from setViewContext.</h1>

We can add to the context for a specific request as well:

let calledOnce = false;
server.ext('onPostHandler', (request, reply) => {
  if (!calledOnce) {
    calledOnce = true;
    // addContext will only add to the context for the current request:
    server.plugins['hapi-view-context'].addContext(request, { someVariable: 'a totally different value' });
  }
  reply.continue();
});

Posting to '/' the first time will now return:

<h1> a totally different value, Another value, a value from the context method, a value from setViewContext.</h1>

But subsequent calls will not have the context that was added to the previous request:

<h1> Some Value, Another value, a value from the context method, a value from setViewContext.</h1>

See /test/test.context.js for more examples and usage.

Readme

Keywords

Package Sidebar

Install

npm i hapi-view-context

Weekly Downloads

265

Version

5.0.2

License

ISC

Unpacked Size

16.9 kB

Total Files

12

Last publish

Collaborators

  • jga
  • dawnerd
  • ecwillis
  • orthagonal1
  • alaguna
  • aleperez92