refactor(injector): implement support for nested bindings

This commit is contained in:
vsavkin 2014-10-09 12:18:35 -04:00
parent 0b60f8494d
commit 3f3fb7017e
3 changed files with 15 additions and 4 deletions

View File

@ -20,7 +20,7 @@ function _isWaiting(obj):bool {
export class Injector {
constructor(bindings:List) {
var flatten = _flattenBindings(bindings);
var flatten = _flattenBindings(bindings, MapWrapper.create());
this._bindings = this._createListOfBindings(flatten);
this._instances = this._createInstances();
this._parent = null; //TODO: vsavkin make a parameter
@ -227,8 +227,7 @@ class _AsyncInjectorStrategy {
}
function _flattenBindings(bindings:List) {
var res = {};
function _flattenBindings(bindings:List, res:Map) {
ListWrapper.forEach(bindings, function (b) {
if (b instanceof Binding) {
MapWrapper.set(res, b.key.id, b);
@ -237,6 +236,9 @@ function _flattenBindings(bindings:List) {
var s = bind(b).toClass(b);
MapWrapper.set(res, s.key.id, s);
} else if (b instanceof List) {
_flattenBindings(b, res);
} else if (b instanceof BindingBuilder) {
throw new InvalidBindingError(b.token);

View File

@ -133,6 +133,15 @@ export function main() {
expect(car.engine).toBeAnInstanceOf(Engine);
});
it("should flatten passed-in bindings", function () {
var injector = new Injector([
[[Engine, Car]]
]);
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(Car);
});
it('should use non-type tokens', function () {
var injector = new Injector([
bind('token').toValue('value')

View File

@ -3,7 +3,7 @@ export var Map = window.Map;
export var Set = window.Set;
export class MapWrapper {
static create():HashMap { return new HashMap(); }
static create():Map { return new Map(); }
static get(m, k) { return m[k]; }
static set(m, k, v) { m[k] = v; }
static contains(m, k) { return m[k] != undefined; }