-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng.inject.js
100 lines (91 loc) · 4.11 KB
/
ng.inject.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
/* global angular*/
angular.module('ngInjectJS', ['injectJS'], ['$provide', '$injectorProvider', '$injectJSProvider', function ($provide, $injectorProvider) {
var originalServiceProvider = $provide.service,
$injectJS;
var $injector = $injectorProvider.$get();
$injectJS = $injector.get('$injectJS');
$provide.service = function (name, fn, lifetime) {
if (lifetime && lifetime !== 'singleton') {
if (lifetime === 'state') {
originalServiceProvider.apply(this, arguments);
$provide.decorator(name, [
'$delegate', function($delegate) {
var proxy = {},
prop;
for (var key in $delegate) {
if ($delegate.hasOwnProperty(key)) {
(function(key) {
if (typeof prop === 'function') {
proxy[key] = function() {
var actual = $injectJS.get(name);
return actual[key].apply(actual, arguments);
};
} else {
Object.defineProperty(proxy, key, {
get: function() {
return $injectJS.get(name)[key];
},
set: function(value) {
$injectJS.get(name)[key] = value;
}
});
}
}(key));
}
}
return proxy;
}
]);
}
return $injectJS.registerType(name, fn, lifetime);
}
return originalServiceProvider.apply(this, arguments);
};
var originalInvoke = $injector.invoke;
$injectJS.get = function(name, serviceName) {
var descriptor = this.fakes[name] || this.types[name] || this.providers[name];
if (!descriptor) {
throw 'There is no dependency named "' + name + '" registered.';
}
var adhoc = {};
var dependencies = descriptor.dependencies || [];
var length = dependencies.length;
for (var i = 0; i < length; i++) {
var current = dependencies[i];
if ($injector.has(current)) {
adhoc[current] = $injector.get(current);
}
}
return this._inject(name, serviceName)(adhoc);
};
$injector.invoke = function (fn, self, locals, serviceName) {
if (typeof locals === 'string') {
serviceName = locals;
locals = null;
}
return originalInvoke.call($injector, fn, self, getInjectJsDependencies(locals, fn, serviceName), serviceName);
};
var originalInstantiate = $injector.instantiate;
$injector.instantiate = function(fn, locals, serviceName) {
return originalInstantiate.call($injector, fn, getInjectJsDependencies(locals, fn, serviceName), serviceName);
};
function getInjectJsDependencies(locals, fn, serviceName) {
var $inject = $injector.annotate(fn),
args = locals || {},
skip = ['$scope', '$element', '$attrs'];
for (var i = 0; i < $inject.length; i++) {
var current = $inject[i];
if (!(~skip.indexOf(current) || $injector.has(current) || args[current])) {
args[current] = $injectJS.get(current, serviceName);
}
}
return args;
}
var originalGet = $injector.get;
$injector.get = function (name, caller) {
if (!this.has(name)) {
return $injectJS.get(name, caller)
}
return originalGet.apply(this, arguments)
}
}]);