Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for global view context #6

Merged
merged 3 commits into from Oct 31, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -54,7 +54,8 @@ internals.schema.viewBase = internals.schema.viewOverride.keys({
helpersPath: Joi.string(),
isCached: Joi.boolean(),
compileMode: Joi.string().valid('sync', 'async'),
defaultExtension: Joi.string()
defaultExtension: Joi.string(),
context: [ Joi.object(), Joi.func() ]
});


@@ -101,6 +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._engines = {};
this._defaultExtension = defaultExtension || (extensions.length === 1 ? extensions[0] : '');

@@ -259,9 +261,17 @@ internals.Manager.prototype.render = function (filename, context, options, callb

var self = this;

context = context || {};
options = options || {};

var defaultContext;
if (typeof this._context === 'function') {
defaultContext = this._context();
}
else {
defaultContext = this._context ? Hoek.clone(this._context) : {};
}

This comment has been minimized.

Copy link
@hueniverse

hueniverse Nov 1, 2014

Member

This block should be a single line statement.

var defaultContext = (this._context ? (typeof this._context === 'function' ? this._context() : Hoek.clone(this._context)) : {});

This comment has been minimized.

Copy link
@jagoda

jagoda Nov 1, 2014

Author Contributor

OK. Is there something in the style guide that I missed that applies to this?

This comment has been minimized.

Copy link
@hueniverse

hueniverse Nov 2, 2014

Member

Nope. just cleaner and most consistent.

context = Hoek.merge(defaultContext, context, true, false);

var engine = null;

var fileExtension = Path.extname(filename).slice(1);
@@ -641,6 +641,108 @@ describe('Manager', function () {
});
});

it('renders with a global context object', function (done) {

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

context: {
message: 'default message',

query: {
test: 'global'
}
}
});

testView.render('valid/testContext', null, null, function (err, rendered, config) {

expect(rendered).to.exist;
expect(rendered).to.contain('<h1>global</h1>');
expect(rendered).to.contain('<h1>default message</h1>');
done();
});
});

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

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

context: {
message: 'default message',

query: {
test: 'global'
}
}
});

testView.render('valid/testContext', { message : 'override' }, null, function (err, rendered, config) {

expect(rendered).to.exist;
expect(rendered).to.contain('<h1>global</h1>');
expect(rendered).to.contain('<h1>override</h1>');
done();
});
});

it('renders with a global context function', function (done) {

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

context: function () {

return {
message: 'default message',

query: {
test: 'global'
}
};
}
});

testView.render('valid/testContext', null, null, function (err, rendered, config) {

expect(rendered).to.exist;
expect(rendered).to.contain('<h1>global</h1>');
expect(rendered).to.contain('<h1>default message</h1>');
done();
});
});

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

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

context: function () {

return {
message: 'default message',

query: {
test: 'global'
}
};
}
});

testView.render('valid/testContext', { message : 'override' }, null, function (err, rendered, config) {

expect(rendered).to.exist;
expect(rendered).to.contain('<h1>global</h1>');
expect(rendered).to.contain('<h1>override</h1>');
done();
});
});

it('uses specified default ext', function (done) {

var testView = new Vision.Manager({
@@ -1190,6 +1292,84 @@ describe('Manager', function () {
done();
});
});

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

var server = new Hapi.Server();
server.handler('viewTest', Vision.handler);
server._views = new Vision.Manager({
engines: { html: require('handlebars') },
path: __dirname + '/templates',

context: {
message: 'default message',

query: {
test: 'global'
}
}
});

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

expect(res.result).to.contain('<h1>global</h1>');
expect(res.result).to.contain('<h1>default message</h1>');
done();
});
});

it('overrides the global context with the default handler context', function (done) {

var server = new Hapi.Server();
server.handler('viewTest', Vision.handler);
server._views = new Vision.Manager({
engines: { html: require('handlebars') },
path: __dirname + '/templates',

context: {
message: 'default message',

query: {
test: 'global'
}
}
});

server.route({ method: 'GET', path: '/', handler: { viewTest: { template: 'valid/testContext' } } });
server.inject('/?test=yes', function (res) {

expect(res.result).to.contain('<h1>yes</h1>');
expect(res.result).to.contain('<h1>default message</h1>');
done();
});
});

it('overrides the global and default contexts with a custom handler context', function (done) {

var server = new Hapi.Server();
server.handler('viewTest', Vision.handler);
server._views = new Vision.Manager({
engines: { html: require('handlebars') },
path: __dirname + '/templates',

context: {
message: 'default message',

query: {
test: 'global'
}
}
});

server.route({ method: 'GET', path: '/', handler: { viewTest: { template: 'valid/testContext', context: { message: 'override', query: { test: 'no' } } } } });
server.inject('/?test=yes', function (res) {

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

describe('response()', function () {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.