diff options
| author | Misko Hevery | 2011-10-26 20:54:45 -0700 |
|---|---|---|
| committer | Misko Hevery | 2011-11-14 16:39:32 -0800 |
| commit | 411c1ae77eaeef1686274e9e4995641a8f83e765 (patch) | |
| tree | 8d72f5b5ad3e3684a16420d6e433cc41f500a55a | |
| parent | d12df0d360fe0dabdca3591654327834bee2803b (diff) | |
| download | angular.js-411c1ae77eaeef1686274e9e4995641a8f83e765.tar.bz2 | |
feat(injector): support ['$service', function($service){}] annotations for function invocation.
| -rw-r--r-- | src/Injector.js | 13 | ||||
| -rw-r--r-- | test/InjectorSpec.js | 49 |
2 files changed, 49 insertions, 13 deletions
diff --git a/src/Injector.js b/src/Injector.js index a3ce7f10..3a7fdeda 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -59,8 +59,17 @@ function createInjector(factories) { function invoke(self, fn, args, path){ args = args || []; - var injectNames = fn.$inject || []; - var i = injectNames.length; + var injectNames; + var i; + if (typeof fn == 'function') { + injectNames = fn.$inject || []; + i = injectNames.length; + } else if (fn instanceof Array) { + injectNames = fn; + i = injectNames.length; + fn = injectNames[--i]; + } + assertArgFn(fn, 'fn'); while(i--) { args.unshift(injector(injectNames[i], path)); } diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index fe1993e5..46c9897b 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -18,17 +18,6 @@ describe('injector', function() { expect(injector('instance')).toEqual(original); }); - it("should call function", function() { - providers('a', function() {return 1;}); - providers('b', function() {return 2;}); - var args; - function fn(a, b, c, d) { - args = [this, a, b, c, d]; - } - fn.$inject = ['a', 'b']; - injector.invoke({name:"this"}, fn, [3, 4]); - expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); - }); it('should inject providers', function() { providers('a', function() {return 'Mi';}); @@ -82,6 +71,44 @@ describe('injector', function() { expect(injector('eager')).toBe('foo'); }); + describe('invoke', function(){ + var args; + + beforeEach(function(){ + args = null; + providers('a', function() {return 1;}); + providers('b', function() {return 2;}); + }); + + + function fn(a, b, c, d) { + args = [this, a, b, c, d]; + } + + + it('should call function', function() { + fn.$inject = ['a', 'b']; + injector.invoke({name:"this"}, fn, [3, 4]); + expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); + }); + + + it('should treat array as annotations', function(){ + injector.invoke({name:"this"}, ['a', 'b', fn], [3, 4]); + expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); + }); + + + it('should fail with errors if not function or array', function(){ + expect(function(){ + injector.invoke({}, {}); + }).toThrow("Argument 'fn' is not a function, got Object"); + expect(function(){ + injector.invoke({}, ['a', 123]); + }).toThrow("Argument 'fn' is not a function, got number"); + }); + }); + describe('annotation', function() { it('should return $inject', function() { function fn() {} |
