diff --git a/public/js/InjectionMapping.js b/public/js/InjectionMapping.js index fc71c0a..b1d31e9 100644 --- a/public/js/InjectionMapping.js +++ b/public/js/InjectionMapping.js @@ -5,6 +5,7 @@ injector.InjectionMapping = function(type, name, id) { this._value = null; this._toType = null; + this._isSingleton = false; this._isValid = function() { return this._value!=null || this._toType!=null; @@ -38,10 +39,11 @@ injector.InjectionMapping.prototype = { }, toSingleton: function(type) { - this.toValue(new type()); + this.toType(type); + this._isSingleton = true; }, - getValue: function() { + getValue: function(injector) { if(!this._isValid()) { throw new Error("Could not get value for "+this._id+" because the mapping is invalid"); return; @@ -50,7 +52,12 @@ injector.InjectionMapping.prototype = { if(this._value!=null) { return this._value; } else if(this._toType!=null) { - return new this._toType(); + var value = new this._toType(); + injector.injectInto(value); + if(this._isSingleton) { + this._value = value; + } + return value; } } }; \ No newline at end of file diff --git a/public/js/Injector.js b/public/js/Injector.js index 82bcae6..370113f 100644 --- a/public/js/Injector.js +++ b/public/js/Injector.js @@ -71,7 +71,7 @@ injector.Injector.prototype = { getInstance: function(type, name) { if(this.hasMapping(type, name)) { - return this.getMapping(type, name).getValue(); + return this.getMapping(type, name).getValue(this); } else { var nameError = name == undefined ? "" : " by name "+ name; throw new Error("Cannot return instance \"" + type + nameError + "\" because no mapping has been found"); diff --git a/spec/javascripts/injectorSpec.js b/spec/javascripts/injectorSpec.js index f46905f..4b8a684 100644 --- a/spec/javascripts/injectorSpec.js +++ b/spec/javascripts/injectorSpec.js @@ -206,6 +206,37 @@ describe("Injector", function() { expect(function() {injector.getInstance('someValue2')}).toThrow(new Error('Cannot return instance "someValue2" because no mapping has been found')); }); + it("injects into returned instance when mapped with toType", function() { + var someValue = 'someValueToInject'; + var SomeObject = function() { + this.someValue = 'inject'; + }; + injector.map('someValue').toValue(someValue); + injector.map('SomeObject').toType(SomeObject); + expect(injector.getInstance('SomeObject').someValue).toBe(someValue); + }); + + it("injects into returned instance when mapped with toSingleton", function() { + var someValue = 'someValueToInject'; + var SomeObject = function() { + this.someValue = 'inject'; + }; + injector.map('someValue').toValue(someValue); + injector.map('SomeObject').toSingleton(SomeObject); + expect(injector.getInstance('SomeObject').someValue).toBe(someValue); + }); + + it("instantiates the singleton on demand", function() { + var someValue = 'someValueToInject'; + var SomeObject = function() { + this.someValue = 'inject'; + }; + injector.map('SomeObject').toSingleton(SomeObject); + injector.map('someValue').toValue(someValue); + expect(injector.getInstance('SomeObject').someValue).toBe(someValue); + }); + + describe("childInjector", function() { it("defaults to null when it was not instantiated by a parent", function() {