Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions public/js/InjectionMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
};
2 changes: 1 addition & 1 deletion public/js/Injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
31 changes: 31 additions & 0 deletions spec/javascripts/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down