-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency.js
96 lines (77 loc) · 2.88 KB
/
dependency.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
(function(angular) {
function supportObject(delegate) {
return function(dependency, path) {
if (angular.isArray(dependency)) {
angular.forEach(dependency, function(name) { return delegate(name); });
} else if (angular.isObject(dependency)) {
angular.forEach(dependency,
function(path, name) { return delegate(name, path); });
} else
return delegate(dependency, path);
};
}
// Initialize an external ng instance for familiar testing
var ng = angular.bootstrap(),
$http = ng.get('$http'),
$timeout = ng.get('$timeout'),
$document = ng.get('$document'),
$exceptionhandler = ng.get('$exceptionHandler'),
module = angular.module,
modules = {},
dependencies = [];
dependencies.unresolved = {};
dependencies.count = 0;
dependencies.defer = function(dependency, path) {
if (arguments[1] === undefined) {
// Mark module for later loading
this.unresolved[dependency] = true;
// Load module
} else if (!~dependencies.indexOf(dependency)) {
$http.get(path)
.success(function(module) {
var script = angular.element('<script>').html(module);
$document.find('head').append(script);
$timeout(dependencies.resolve.bind(dependencies), 0);
})
;
this.push(dependency);
this.count += 1;
// Mark module as loaded
delete this.unresolved[dependency];
// Defer bootstraping until all dependencies are loaded
if (!window.name.match(/^NG_DEFER_BOOTSTRAP!/))
window.name = 'NG_DEFER_BOOTSTRAP!' + window.name;
}
};
dependencies.resolve = function() {
if (!--this.count) {
for (var module in this.unresolved)
$exceptionHandler(
Error(module +' has no path'), 'All dependencies must include paths');
// Only resume bootstrapping if all modules have loaded
if (!module)
angular.resumeBootstrap(dependencies);
}
};
// Expose to angular to allow modularization
angular.dependency = supportObject(dependencies.defer.bind(dependencies));
// Decorate the moduleInstance to give access to dependency loading
angular.module = function(name) {
// Check and update already loaded modules
if (!modules[name] && (modules[name] = true))
if (arguments.length === 1)
// Conform to ng syntax
arguments = [name, []];
var moduleInstance = module.apply(angular, arguments);
moduleInstance.dependency = function(dependency, path) {
supportObject(addDependency)(dependency, path);
// .dependency is chainable
return moduleInstance;
function addDependency(dependency, path) {
moduleInstance.requires.push(dependency);
dependencies.defer(dependency, path);
}
};
return moduleInstance;
};
}(angular) );