diff options
| author | Vojta Jina | 2012-02-08 16:12:11 -0800 | 
|---|---|---|
| committer | Vojta Jina | 2012-02-08 16:12:11 -0800 | 
| commit | 776739299b698a965ef818eeda75d4eddd10c491 (patch) | |
| tree | efe64f6d01850b16c67bb1460a044925ac99a605 | |
| parent | 3173d8603db4ae1c2373e13a7a490988126bb1e7 (diff) | |
| download | angular.js-776739299b698a965ef818eeda75d4eddd10c491.tar.bz2 | |
fix($injector): instantiate returns instance, if non-object value returned from constructor
| -rw-r--r-- | src/Injector.js | 11 | ||||
| -rw-r--r-- | test/InjectorSpec.js | 27 | 
2 files changed, 32 insertions, 6 deletions
| diff --git a/src/Injector.js b/src/Injector.js index 94154bec..f5e35d0f 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -429,12 +429,15 @@ function createInjector(modulesToLoad) {        }      } -    function instantiate(Type, locals){ -      var Constructor = function(){}, -          instance; +    function instantiate(Type, locals) { +      var Constructor = function() {}, +          instance, returnedValue; +        Constructor.prototype = Type.prototype;        instance = new Constructor(); -      return invoke(Type, instance, locals) || instance; +      returnedValue = invoke(Type, instance, locals); + +      return isObject(returnedValue) ? returnedValue : instance;      }      return { diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index 6e9a98f2..dea76a6b 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -606,8 +606,12 @@ describe('injector', function() {      it('should allow constructor to return different object', function() { -      var t = $injector.instantiate(function() { return 'ABC'; }); -      expect(t).toBe('ABC'); +      var obj = {}; +      var Class = function() { +        return obj; +      }; + +      expect($injector.instantiate(Class)).toBe(obj);      }); @@ -616,6 +620,25 @@ describe('injector', function() {          $injector.instantiate(function() { throw 'MyError'; });        }).toThrow('MyError');      }); + + +    it('should return instance if constructor returns non-object value', function() { +      var A = function() { +        return 10; +      }; + +      var B = function() { +        return 'some-string'; +      }; + +      var C = function() { +        return undefined; +      }; + +      expect($injector.instantiate(A) instanceof A).toBe(true); +      expect($injector.instantiate(B) instanceof B).toBe(true); +      expect($injector.instantiate(C) instanceof C).toBe(true); +    });    });    describe('protection modes', function() { | 
