-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 2.26 KB
/
Copy pathindex.js
File metadata and controls
69 lines (59 loc) · 2.26 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var path = require('path');
var util = require('racer/lib/util');
var derbyTemplates = require('derby-templates');
var templates = derbyTemplates.templates;
var Component = require('derby/lib/components').Component;
var createFactory = require('derby/lib/components').createFactory;
module.exports = function (derby, options) {
var App = derby.App;
App.prototype.component = function(viewName, constructor, ns) {
if (typeof viewName === 'function') {
constructor = viewName;
viewName = null;
}
// Inherit from Component
extendComponent(constructor);
// Set namespace to the component if it's passed along
if(ns) constructor.prototype.ns = ns;
// Load template view from filename
if (constructor.prototype.view) {
var viewFilename = constructor.prototype.view;
viewName = constructor.prototype.name || path.basename(viewFilename, '.html');
if(ns) viewName = ns + ':' + viewName;
this.loadViews(viewFilename, viewName);
} else if (!viewName) {
if (constructor.prototype.name) {
viewName = constructor.prototype.name;
if(ns) viewName = ns + ':' + viewName;
var view = this.views.register(viewName);
view.template = templates.emptyTemplate;
} else {
throw new Error('No view name specified for component');
}
}
// Load sub components
var subComponents = constructor.prototype.components;
if (subComponents) {
for(var i = 0, len = subComponents.length; i < len; i++) {
this.component(null, subComponents[i], viewName);
}
}
// Associate the appropriate view with the component type
var view = this.views.find(viewName);
if (!view) {
var message = this.views.findErrorMessage(viewName);
throw new Error(message);
}
view.componentFactory = createFactory(constructor);
// Make chainable
return this;
};
function extendComponent(constructor) {
// Don't do anything if the constructor already extends Component
if (constructor.prototype instanceof Component) return;
// Otherwise, replace its prototype with an instance of Component
var oldPrototype = constructor.prototype;
constructor.prototype = new Component();
util.mergeInto(constructor.prototype, oldPrototype);
}
};