mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-10 07:44:43 +00:00
feat(injector): support $inject(fn($service){}) function invocation
This commit is contained in:
parent
411c1ae77e
commit
9062996a0e
2 changed files with 23 additions and 11 deletions
|
|
@ -33,7 +33,9 @@
|
||||||
* `injector.eager()`
|
* `injector.eager()`
|
||||||
*/
|
*/
|
||||||
function createInjector(factories) {
|
function createInjector(factories) {
|
||||||
var instanceCache = {$injector: injector};
|
var instanceCache = {
|
||||||
|
$injector: injector
|
||||||
|
};
|
||||||
factories = factories || angularService;
|
factories = factories || angularService;
|
||||||
|
|
||||||
injector.invoke = invoke;
|
injector.invoke = invoke;
|
||||||
|
|
@ -42,19 +44,23 @@ function createInjector(factories) {
|
||||||
if (factory.$eager)
|
if (factory.$eager)
|
||||||
injector(name);
|
injector(name);
|
||||||
});
|
});
|
||||||
return injector;
|
return instanceCache.$injector;
|
||||||
|
|
||||||
function injector(serviceId, path){
|
function injector(serviceId, path){
|
||||||
if (!(serviceId in instanceCache)) {
|
if (typeof serviceId == 'string') {
|
||||||
var factory = factories[serviceId];
|
if (!(serviceId in instanceCache)) {
|
||||||
path = path || [];
|
var factory = factories[serviceId];
|
||||||
path.unshift(serviceId);
|
path = path || [];
|
||||||
if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
|
path.unshift(serviceId);
|
||||||
inferInjectionArgs(factory);
|
if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
|
||||||
instanceCache[serviceId] = invoke(null, factory, [], path);
|
inferInjectionArgs(factory);
|
||||||
path.shift();
|
instanceCache[serviceId] = invoke(null, factory, [], path);
|
||||||
|
path.shift();
|
||||||
|
}
|
||||||
|
return instanceCache[serviceId];
|
||||||
|
} else {
|
||||||
|
return invoke(null, serviceId, path);
|
||||||
}
|
}
|
||||||
return instanceCache[serviceId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function invoke(self, fn, args, path){
|
function invoke(self, fn, args, path){
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,7 @@ describe('injector', function() {
|
||||||
|
|
||||||
function fn(a, b, c, d) {
|
function fn(a, b, c, d) {
|
||||||
args = [this, a, b, c, d];
|
args = [this, a, b, c, d];
|
||||||
|
return a + b + c + d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -99,6 +100,11 @@ describe('injector', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should invoke the passed in function with all of the dependencies as arguments', function(){
|
||||||
|
expect(injector(['a', 'b', fn], [3, 4])).toEqual(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should fail with errors if not function or array', function(){
|
it('should fail with errors if not function or array', function(){
|
||||||
expect(function(){
|
expect(function(){
|
||||||
injector.invoke({}, {});
|
injector.invoke({}, {});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue