-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRlModule.js
68 lines (62 loc) · 2.13 KB
/
RlModule.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
/**
* A replacement for angular.module - this will bundle namespaced components together
* in an awesome way.
*
* Given a bunch of 'nested' modules:
* angular.rlmodule('myapp.controllers.FooCtrl', []).controller('FooCtrl', ...);
* angular.rlmodule('myapp.controllers.BarCtrl', []).controller('BarCtrl', ...);
* angular.rlmodule('myapp.directives.rlDaFunk', []).directive('rlDaFunk', ...);
*
* When I reference a parent module:
* angular.module('myapp.controllers');
* OR
* angular.module('myapp');
* OR
* angular.rlmodule('myapp.controllers.AllDirectives', ['myapp.directives']).controller('AllDirectives', ...);
* OR
* <body ng-app='myapp'></body>
*
* Then I should get all components declared on child modules
**/
(function() {
// These are the modules that are explicitly defined
// These must not be clobbered when we compile the namespaces
// Key: Module name
// Value: Module's dependencies
// {'moduleName': ['dep1', 'dep2'], ...}
var modules = {};
var namespaces = {};
function module(moduleName, depsArray) {
var args = Array.prototype.slice.call(arguments);
modules[moduleName] = depsArray;
addNamespace(moduleName);
var module = angular.module.apply(null, args);
buildModuleBundle();
return module;
};
function addNamespace(moduleName) {
var pieces = moduleName.split('.');
pieces.pop();
var ns = [];
for (var i = 0; i < pieces.length; i++) {
ns.push(pieces[i]);
var nsString = ns.join('.');
if (!namespaces[nsString]) {
namespaces[nsString] = [];
}
namespaces[nsString].push(moduleName);
}
}
function buildModuleBundle() {
for (var ns in namespaces) {
var deps = namespaces[ns];
// Do we have an explicitly declared module at this level?
// If so, include it's dependencies.
if (modules[ns]) {
deps = deps.concat(modules[ns]);
}
angular.module(ns, deps);
}
}
angular.rlmodule = module;
}());