-
Notifications
You must be signed in to change notification settings - Fork 3
/
moduleFactory.js
117 lines (99 loc) · 3.66 KB
/
moduleFactory.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
ModuleFactory = function ModuleFactory (name) {
//TODO: "factories" should read "recepies" and "layerFactories" should read "layerRecepies"
var manager = new AMDManager();
var self = this;
self.instancesByName = {};
self.layerFactories = {};
self.factories = [];
self.plugins = [ '$instance', '$ready', '$template' ];
self.require = wrapRequire(manager);
self.define = wrapDefine(manager);
self.name = name;
self._requests = {};
self.hasConfig = function () {
return !!manager.get('$config');
}
}
ModuleFactory.prototype.addToRecipies = function (factory, options) {
var module = this;
//----------------------
options = options || {};
options.type = options.type || 'factory';
module.factories.push({
type: options.type,
body: factory,
});
_.each(_.keys(module.instancesByName), function (name) {
applyFactory(factory, name, module);
});
};
ModuleFactory.prototype.addLayerFactory = function (layerName, options) {
var module = this;
//--------------------------------------
if (!module.layerFactories[layerName]) {
module.layerFactories[layerName] = [];
}
module.layerFactories[layerName].push({
type: options.type || 'factory',
deps: options.deps || [],
name: options.name,
body: options.body,
});
};
ModuleFactory.prototype.compileLayer = function (layerName) {
var module = this, deps = [];
//-----------------------------------------------------------
_.each(module.layerFactories[layerName], function (factory) {
Array.prototype.push.apply(deps, factory.deps);
});
deps = _.map(_.unique(deps), function (name) { return JSON.stringify(name); });
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
return 'Module(' + JSON.stringify(module.name) + ').define(' + JSON.stringify(layerName) + ', [ "$instance", ' + deps.join(', ') + '], function ($instance) {\n\n' +
_.map(module.layerFactories[layerName], function (factory) {
if (factory.type === 'template') {
return "$instance.__addTemplate__(" + JSON.stringify(factory.name) + ", " + factory.body.toString() + ");";
}
return '$instance.__useFactory__(' + factory.body.toString() + ');';
}).join('\n\n') + '\n\n' +
'});';
};
ModuleFactory.prototype.instantiate = function (instanceName, settings) {
var module = this;
//-----------------------------------------
if (instanceName in module.instancesByName)
throw new Error('instance ' + instanceName + ' already exists');
var instance;
var manager = new AMDManager();
var useAjax = false;
if (Meteor.isClient) {
manager.onModuleNotFound(function (__module__) {
var requestURL = '/modules/' + module.name + '/' + __module__.name;
if (useAjax && !module._requests[requestURL]) {
module._requests[requestURL] = true;
$.ajax({
url : requestURL,
type : 'GET', dataType : 'script',
}).done(function (message) {});
}
});
Meteor.startup(function () {
useAjax = true;
});
}
settings = settings || {};
settings.__module__ = module.name;
settings.__name__ = instanceName;
instance = module.instancesByName[instanceName] = {
settings : settings,
require : wrapRequire(manager),
define : wrapDefine(manager),
// we use this one for lazy loading
__useFactory__ : function (factory) {
applyFactory(factory, instance, module);
},
};
_.each(module.factories, function (factory) {
applyFactory(factory.body, instanceName, module);
});
return module.instancesByName[instanceName];
};