-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (36 loc) · 1.89 KB
/
Copy pathindex.js
File metadata and controls
49 lines (36 loc) · 1.89 KB
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
var _ = require('lodash');
var components = [];
module.exports = function (app) {
['get', 'post', 'put', 'del', 'enter', 'exit'].forEach(function (method) {
app['_' + method] = app[method];
app[method] = function (pattern, component, options) {
var options = options || {};
var viewName = options.viewName || null;
var ns = options.ns || null;
// If no options/viewName is passed along, and no view is specified on the component prototype, then assume regular route.
if(arguments.length === 2 && !viewName && !component.prototype.view) return app['_' + method].apply(this, arguments);
// Don't accept components without a view specified
if(!component.prototype.view) throw new Error('Components must have views specified when in routes!');
// Register component, if it's not already been added or we're forcing it
if(_.indexOf(components, component) < 0 || options.force) {
this.component(viewName, component, ns);
// Add component to component list to avoid adding it twice
components.push(component);
}
app['_' + method].call(this, pattern, function (page, model, params, next) {
var viewName = viewName || component.prototype.name;
// 'get' collides with Derby-level defined method already on component's constructor (prototype), thus, use load instead of get and use the method for the remaining
var _method = (method === 'get' ? 'load' : method);
// No load fn existed, let's just render
if(!component.prototype[_method]) return page.render(viewName);
// Overwrite page in order to be able to overwrite the render fn to
var _page = _.clone(page);
_page.render = function (ns) {
var ns = ns || viewName;
page.render(ns);
};
component.prototype[_method].call(this, _page, model, params, next);
});
}
});
};