-
Notifications
You must be signed in to change notification settings - Fork 2k
/
onRoute.js
78 lines (68 loc) · 2.93 KB
/
onRoute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Module dependencies
*/
var path = require('path');
var _ = require('@sailshq/lodash');
/**
* onRoute()
*
* This is used for handling `route:typeUnknown` events; i.e. to
* support { view: 'foo/bar' } notation. This "teaches" the router
* to understand `view` in route target syntax. This allows route
* addresses to be bound directly to serve specific views without
* going through a custom action.
*
* e.g.
* ```
* 'get /': { view: 'pages/homepage' }
* ```
*
* @param {Sails} sails
* @param {Dictionary} route
* combined route target + route address dictionary
*/
module.exports = function onRoute (sails, route) {
// Ignore unknown route syntax
if ( !_.isPlainObject(route.target) || !_.isString(route.target.view) ) {
// If it needs to be understood by another hook, the hook would have also received
// the typeUnknown event, so we don't need to do anything here.
return;
}
// Ensure there isn't a `.` in the view name.
// (This limitation will be improved in a future version of Sails.)
else if (route.target.view.match(/\./)) {
sails.log.error('Ignoring attempt to bind route (`%s`) to a view with a `.` in the name (`%s`).',route.path, route.target.view);
return;
}
// Otherwise construct an action function which serves a view and then bind it to the route.
else {
// Merge target into `options` to get hold of relevant route options:
route.options = _.merge(route.options, route.target);
// Note: this (^) could be moved up into lib/router/bind.js, since its
// only pertinent for core options such as `skipAssets`. There would need
// to be changes in other hooks as well.
// Transform the view path into something Lodash _.get will understand (i.e. dots not slashes)
var transformedViewAddress = route.target.view.replace(/\//g, '.');
var referenceInViewsHash = _.get(sails.views, transformedViewAddress);
// Look up the view in our views hash and see if it is `true`
// (i.e. indicating it is a view template file)
if (referenceInViewsHash === true) {
return sails.router.bind(route.path, function serveView(req, res) {
return res.view(route.target.view);
}, route.verb, route.options);
}
// Look for a relative `/index` view if the specified view
// is in the views hash as a dictionary (i.e. indicating it is a directory)
else if (_.isObject(referenceInViewsHash) && referenceInViewsHash.index === true) {
var indexViewIdentity = path.join(route.target.view,'/index');
return sails.router.bind(route.path, function serveView(req, res) {
return res.view(indexViewIdentity);
}, route.verb, route.options);
}
// Otherwise, the specified view in this route target doesn't match a
// known view in the project, so ignore the attempt and inform the user.
else {
sails.log.error('Ignoring attempt to bind route (`%s`) to unknown view: `%s`',route.path, route.target.view);
}
}
};