Skip to content

Commit e069a07

Browse files
committed
Add first version
0 parents  commit e069a07

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

example/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var Hapi = require('hapi');
2+
3+
var server = new Hapi.Server();
4+
server.connection({ port: 3000 });
5+
6+
server.register(require('vision'), function (err) {
7+
if (err) {
8+
console.error('Failed to load a plugin:', err);
9+
return;
10+
}
11+
12+
server.views({
13+
engines: {
14+
html: require('handlebars')
15+
},
16+
relativeTo: __dirname,
17+
path: 'templates'
18+
});
19+
});
20+
21+
server.register({
22+
register: require('../'),
23+
options: {
24+
'something': 'Something',
25+
'nested.something': 'Nested'
26+
}
27+
}, function (err) {
28+
if (err) {
29+
console.error('Failed to load a plugin:', err);
30+
return;
31+
}
32+
33+
server.route({
34+
method: 'GET',
35+
path: '/',
36+
handler: function (request, reply) {
37+
reply.view('example');
38+
}
39+
});
40+
41+
server.start(function() {
42+
console.log('Server running at:', server.info.uri);
43+
});
44+
});

example/templates/example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{something}} {{nested.something}}</p>

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var _ = require('lodash');
2+
3+
exports.register = function(server, initial, next) {
4+
var self = this;
5+
6+
this.addContext = function(request, key, data) {
7+
var response = request.response;
8+
9+
if (response.variety !== 'view') return;
10+
11+
response.source.context = _.set(response.source.context || {}, key, data);
12+
13+
return response.source.context;
14+
};
15+
16+
server.expose('addContext', this.addContext);
17+
18+
server.ext('onPreResponse', function(request, reply) {
19+
_.forIn(initial, function(value, key) {
20+
self.addContext(request, key, value);
21+
});
22+
23+
reply.continue();
24+
});
25+
26+
next();
27+
};
28+
29+
exports.register.attributes = {
30+
pkg: require('./package.json')
31+
};

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "hapi-view-context",
3+
"version": "0.0.1",
4+
"description": "Loads data into hapi views",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"hapi",
11+
"views"
12+
],
13+
"author": "First+Third",
14+
"license": "ISC",
15+
"dependencies": {
16+
"lodash": "^3.10.1"
17+
},
18+
"devDependencies": {
19+
"handlebars": "^4.0.3",
20+
"hapi": "^10.1.0",
21+
"hoek": "^2.16.3",
22+
"vision": "^3.0.0"
23+
}
24+
}

readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## hapi-view-context
2+
3+
Loads data into view context
4+
5+
### Installation
6+
7+
`npm install hapi-view-context`
8+
9+
### Usage
10+
11+
```js
12+
server.register({
13+
register: require('hapi-view-context'),
14+
options: {
15+
'someVariable': 'Some Value',
16+
'some.nested.variable': 'Another value'
17+
}
18+
});
19+
```
20+
21+
Options are used to set the default context. The object key will match the context path.
22+
23+
```js
24+
server.ext('onPreResponse', function(request, reply) {
25+
26+
server.plugins['hapi-view-context'].addContext('amazingData', 'More data');
27+
28+
});
29+
```

0 commit comments

Comments
 (0)