Skip to content
Permalink
Browse files

Global context function.

  • Loading branch information
jagoda committed Oct 30, 2014
1 parent 6c11c7b commit 579c9af353a9808c9e6fead9f4d877bf4fa86254
Showing with 65 additions and 4 deletions.
  1. +11 −4 lib/index.js
  2. +54 −0 test/index.js
@@ -55,7 +55,7 @@ internals.schema.viewBase = internals.schema.viewOverride.keys({
isCached: Joi.boolean(),
compileMode: Joi.string().valid('sync', 'async'),
defaultExtension: Joi.string(),
context: Joi.object()
context: [ Joi.object(), Joi.func() ]
});


@@ -102,7 +102,7 @@ exports.Manager = internals.Manager = function (options) {
var extensions = Object.keys(engines);
Hoek.assert(extensions.length, 'Views manager requires at least one registered extension handler');

this._context = options.context || {};
this._context = options.context;
this._engines = {};
this._defaultExtension = defaultExtension || (extensions.length === 1 ? extensions[0] : '');

@@ -264,8 +264,10 @@ internals.Manager.prototype.render = function (filename, context, options, callb
context = context || {};
options = options || {};

var defaultContext = Hoek.clone(this._context);
context = Hoek.merge(defaultContext, context, true, false);
if (typeof this._context === 'object') {
var defaultContext = Hoek.clone(this._context);
context = Hoek.merge(defaultContext, context, true, false);
}

var engine = null;

@@ -459,6 +461,11 @@ internals.Manager.prototype.response = function (template, context, options, req

Joi.assert(options, internals.schema.viewOverride);

if (typeof this._context === 'function') {
var defaultContext = this._context(request);
context = Hoek.merge(defaultContext, context, true, false);
}

var source = {
manager: this,
template: template,
@@ -1294,5 +1294,59 @@ describe('Manager', function () {
done();
});
});

it('uses a global context function', function (done) {

var server = new Hapi.Server();

var handler = function (request, reply) {

var views = new Vision.Manager({
engines: { html: require('handlebars') },
path: __dirname + '/templates/valid',

context: function (request) {
return { message: 'path: ' + request.path };
}
});

var response = views.response('test.html', null, {}, request);
return reply(response);
};

server.route({ method: 'GET', path: '/', handler: handler });
server.inject('/', function (res) {

expect(res.payload).to.contain('<h1>path: /</h1>');
done();
});
});

it('overrides the global context function with local values', function (done) {

var server = new Hapi.Server();

var handler = function (request, reply) {

var views = new Vision.Manager({
engines: { html: require('handlebars') },
path: __dirname + '/templates/valid',

context: function (request) {
return { message: 'path: ' + request.path };
}
});

var response = views.response('test.html', { message: 'override' }, {}, request);
return reply(response);
};

server.route({ method: 'GET', path: '/', handler: handler });
server.inject('/', function (res) {

expect(res.payload).to.contain('<h1>override</h1>');
done();
});
});
});
});

0 comments on commit 579c9af

Please sign in to comment.
You can’t perform that action at this time.