angular/angular — pull request #152 by vsavkin (2014-11-05 01:06:10+00:00)
Title: refactor(ElementInjector): use `index` instead of the `elementInjector` ...

...field to instantiate element injectors

Currently, we use the following strategy to instantiate element injectors:

We walk the tree of proto element injectors

```
for (var i = 0; i < binders.length; ++i) {
injectors[i] = binders[i].protoElementInjector.instantiate();
}
```

and call instantiate

```
instantiate():ElementInjector {
var p = this._parent;
var parentElementInjector = p === null ? null : p._elementInjector;
this._elementInjector = new ElementInjector({
proto: this, parent: parentElementInjector
});
return this._elementInjector;
}
```

After that we clear the `_elementInjector` property. Although this solution works, it forces us to have temporary state (`this._elementInjector`) on `ProtoElementInjector`. This was necessary when we did not have one-to-one correspondence of proto element injectors with element injectors.

@tbosch suggested a better solution:

```
for (var i = 0; i < binders.length; ++i) {
var proto = binders[i].protoElementInjector;
var parent = elementInjectors[proto.parent.index];
injectors[i] = proto.instantiate({parent:parent});
}
```

Here, `ProtoElementInjector` knows its position in the `binders` array. Since there is a one-to-one correspondence between proto element injectors and element injectors, we know the position of the corresponding element injector in the elementInjectors array.