Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 4cd9621

Browse files
Matt Gauntaddyosmani
authored andcommitted
Moving to a JSON api rather html partials. Renaming a few bits and bobs
1 parent 3ba5808 commit 4cd9621

14 files changed

+124
-105
lines changed

gulp-tasks/service-worker.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@ gulp.task('service-worker', function(cb) {
3737
],
3838
dynamicUrlToDependencies: {
3939
'/app-shell': ['server/views/layouts/app-shell.handlebars'],
40-
'/partials/': [
41-
'server/views/layouts/partial.handlebars',
40+
'/api/': [
4241
'server/views/index.handlebars'
4342
],
44-
'/partials/url-1': [
45-
'server/views/layouts/partial.handlebars',
43+
'/api/url-1': [
4644
'server/views/url-1.handlebars'
4745
],
48-
'/partials/url-2': [
49-
'server/views/layouts/partial.handlebars',
46+
'/api/url-2': [
5047
'server/views/url-2.handlebars'
5148
]
5249
},

server/app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
var serverController = require('./controllers/server-controller');
1515
var StaticPageController = require('./controllers/static-page-controller');
16-
var PartialsController = require('./controllers/partials-controller');
16+
var APIController = require('./controllers/api-controller');
1717

18-
// PartialsController serves up the HTML without any HTML body or head
19-
serverController.addEndpoint('/partials*', new PartialsController());
18+
// APIController serves up the HTML without any HTML body or head
19+
serverController.addEndpoint('/api*', new APIController(
20+
serverController.getHandleBarsInstance()
21+
));
2022
// The static page controller serves the basic form of the pages
2123
serverController.addEndpoint('/*', new StaticPageController());

server/controllers/api-controller.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
var pathConfigs = require('../models/path-config.js');
6+
7+
function APIController(handlebarsInstance) {
8+
this.handlebarsInstance = handlebarsInstance;
9+
}
10+
11+
// This method looks at the request path and renders the appropriate handlebars
12+
// template
13+
APIController.prototype.onRequest = function(req, res) {
14+
var urlSections = req.path.split('/');
15+
urlSections = urlSections.filter(function(sectionString) {
16+
return sectionString.length > 0;
17+
});
18+
19+
var urlPath = null;
20+
if (urlSections.length === 1) {
21+
urlPath = '/';
22+
} else {
23+
urlPath = '/' + urlSections[1];
24+
}
25+
26+
var pathConfig = pathConfigs.getConfig(urlPath);
27+
if (!pathConfig) {
28+
res.status(404).send();
29+
return;
30+
}
31+
32+
var viewPath = path.join(
33+
__dirname,
34+
'/../views',
35+
pathConfig.data.view + '.handlebars'
36+
);
37+
38+
this.handlebarsInstance.render(viewPath, pathConfig)
39+
.then(function(renderedTemplate) {
40+
res.json({
41+
title: pathConfig.data.title,
42+
partialinlinestyles: pathConfig.data.inlineStyles,
43+
partialremotestyles: pathConfig.data.remoteStyles,
44+
partialscripts: pathConfig.data.remoteScripts,
45+
partialhtml: renderedTemplate
46+
});
47+
})
48+
.catch(function(err) {
49+
res.status(500).send();
50+
});
51+
};
52+
53+
module.exports = APIController;

server/controllers/partials-controller.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

server/controllers/server-controller.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ var exphbs = require('express-handlebars');
44

55
function ServerController() {
66
var expressApp = express();
7-
var expressServer = this.setUpServer(expressApp);
7+
var handleBarsInstance = exphbs.create({
8+
defaultLayout: 'default',
9+
layoutsDir: path.join(__dirname, '/../views/layouts'),
10+
partialsDir: path.join(__dirname, '/../views/partials')
11+
});
12+
var expressServer = this.setUpServer(expressApp, handleBarsInstance);
813

914
this.getExpressApp = function() {
1015
return expressApp;
@@ -13,16 +18,16 @@ function ServerController() {
1318
this.getExpressServer = function() {
1419
return expressServer;
1520
};
21+
22+
this.getHandleBarsInstance = function() {
23+
return handleBarsInstance;
24+
};
1625
}
1726

18-
ServerController.prototype.setUpServer = function(app) {
27+
ServerController.prototype.setUpServer = function(app, handleBarsInstance) {
1928
// Set up the use of handle bars and set the path for views and layouts
2029
app.set('views', path.join(__dirname, '/../views'));
21-
app.engine('handlebars', exphbs({
22-
defaultLayout: 'default',
23-
layoutsDir: path.join(__dirname, '/../views/layouts'),
24-
partialsDir: path.join(__dirname, '/../views/partials'),
25-
}));
30+
app.engine('handlebars', handleBarsInstance.engine);
2631
app.set('view engine', 'handlebars');
2732

2833
// Define static assets path - i.e. styles, scripts etc.

server/controllers/static-page-controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ StaticPageController.prototype.onRequest = function(req, res) {
2424
return;
2525
default:
2626
// Use default layout
27-
res.render(pathConfig.view, pathConfig);
27+
res.render(pathConfig.data.view, pathConfig);
2828
return;
2929
}
3030
};

server/models/path-config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@ var path = require('path');
44
var pathConfigs = {
55
'/': {
66
view: 'index',
7+
title: 'Index',
78
inlineStyles: getFileContents(['/styles/core.css']),
89
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
910
'400,300,700,500,400italic'],
1011
remoteScripts: ['/scripts/static-page.js']
1112
},
1213
'/url-1': {
1314
view: 'url-1',
15+
title: 'URL 1',
1416
inlineStyles: getFileContents(['/styles/core.css']),
1517
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
1618
'400,300,700,500,400italic'],
1719
remoteScripts: ['/scripts/static-page.js']
1820
},
1921
'/url-2': {
2022
view: 'url-2',
23+
title: 'URL 2',
2124
inlineStyles: getFileContents(['/styles/core.css']),
2225
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
2326
'400,300,700,500,400italic'],
2427
remoteScripts: ['/scripts/static-page.js']
2528
},
2629
'/app-shell': {
2730
view: '',
31+
title: 'App Shell',
2832
inlineStyles: getFileContents(['/styles/core.css']),
2933
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
3034
'400,300,700,500,400italic'],
@@ -53,8 +57,8 @@ module.exports = {
5357
return null;
5458
}
5559

56-
// This needed to ensure changes made to the objects dont stick / alter
57-
// the original object
58-
return Object.create(object);
60+
return {
61+
'data': object
62+
};
5963
}
6064
};

server/views/layouts/partial.handlebars

Lines changed: 0 additions & 9 deletions
This file was deleted.

server/views/partials/async-css.handlebars

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{{#if remoteStyles}}
1+
{{#if data.remoteStyles}}
22
<script>
33
var remoteStyles = [
4-
{{#each remoteStyles}}
4+
{{#each data.remoteStyles}}
55
'{{this}}',
66
{{~/each}}
77
];
@@ -26,7 +26,7 @@
2626

2727
<!-- In case the browser has JS disabled -->
2828
<noscript>
29-
{{#each remoteStyles}}
29+
{{#each data.remoteStyles}}
3030
<link href="{{this}}" rel="stylesheet" property="stylesheet" media="all">
3131
{{~/each}}
3232
</noscript>

server/views/partials/close-page.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
{{> async-css }}
2020

21-
{{#each remoteScripts}}
21+
{{#each data.remoteScripts}}
2222
<script src="{{this}}" async></script>
2323
{{~/each}}
2424
</body>

0 commit comments

Comments
 (0)