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

Better context injection #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ internals.manifest = {
plugin: 'vision'
},

// Views loader
// views and default context
{
plugin: {
register: 'visionary',
register: './lib/views',
options: {
engines: {
hbs: 'handlebars'
views:{
engines: {
hbs: 'handlebars'
},
path: './app/templates',
layoutPath: './app/templates/layouts',
helpersPath: './app/templates/helpers',
partialsPath: './app/templates/partials',
layout: 'default'
},
path: './app/templates',
layoutPath: './app/templates/layouts',
helpersPath: './app/templates/helpers',
partialsPath: './app/templates/partials',
layout: 'default'
misc: {
meta: Meta.get('/'),
}
}
}
},
Expand All @@ -82,13 +87,6 @@ internals.manifest = {
}
},

// Flash Plugin
{
plugin: {
register: './lib/flash'
}
},

// Hapi cookie jar
{
plugin: {
Expand All @@ -105,16 +103,6 @@ internals.manifest = {
}
},

// App context decorator
{
plugin: {
register: './lib/context',
options: {
meta: Meta.get('/')
}
}
},

// Core routes
{
plugin: './app/routes/core.js'
Expand Down
2 changes: 1 addition & 1 deletion lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ exports.register = function(server, options, next) {

exports.register.attributes = {
name: 'auth',
dependencies: ['context', 'hapi-auth-cookie', 'mongoose']
dependencies: ['views', 'hapi-auth-cookie', 'mongoose']
};
29 changes: 0 additions & 29 deletions lib/context.js

This file was deleted.

30 changes: 0 additions & 30 deletions lib/flash.js

This file was deleted.

47 changes: 47 additions & 0 deletions lib/views.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const Hoek = require('hoek');
const internals = {};

internals.prepare = function(config) {

const engines = config.engines;
Object.keys(engines).forEach((engine) => {
if ( typeof engines[engine] === 'string' ) {
engines[engine] = require(engines[engine]);
}
});
return config;
};

exports.register = function(plugin, options, next) {

const ext = {
context: function(request) {

const ctx = {
devEnv: (process.env.NODE_ENV === 'development'),
meta: options.misc.meta,
credentials: request.auth.isAuthenticated?request.auth.credentials:null
};

if ( request.yar && request.yar.flash ) {
ctx.flash = request.yar.flash();
}

return ctx;
}
};

let viewOpts = options.views;
viewOpts = Hoek.merge(viewOpts, ext);
viewOpts = internals.prepare(viewOpts);
plugin.root.views(viewOpts);
return next();
};

exports.register.attributes = {
name: 'views',
version: require('../package.json').version,
depedencies: ['vision']
};
7 changes: 6 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const Hoek = require('hoek');
Composer(function(err, server) {

Hoek.assert(!err, err);
server.start(function() {
server.start(function(err) {

if ( err ) {
throw err;
}

console.log('Server started @ ' + server.info.uri);
});

Expand Down