feat(di): added context to runtime DI errors

This commit is contained in:
vsavkin
2015-07-22 12:00:35 -07:00
parent 8ecb632d70
commit 5a86f85936
6 changed files with 126 additions and 59 deletions

View File

@ -419,6 +419,9 @@ export class ProtoElementInjector {
getBindingAtIndex(index: number): any { return this.protoInjector.getBindingAtIndex(index); }
}
class _Context {
constructor(public element: any, public componentElement: any, public injector: any) {}
}
export class ElementInjector extends TreeNode<ElementInjector> implements DependencyProvider {
private _host: ElementInjector;
@ -438,7 +441,9 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
constructor(public _proto: ProtoElementInjector, parent: ElementInjector) {
super(parent);
this._injector = new Injector(this._proto.protoInjector, null, this);
this._injector =
new Injector(this._proto.protoInjector, null, this, () => this._debugContext());
// we couple ourselves to the injector strategy to avoid polymoprhic calls
var injectorStrategy = <any>this._injector.internalStrategy;
this._strategy = injectorStrategy instanceof InjectorInlineStrategy ?
@ -489,6 +494,12 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
this.hydrated = true;
}
private _debugContext(): any {
var p = this._preBuiltObjects;
return new _Context(p.elementRef.nativeElement, p.view.getHostElement().nativeElement,
this._injector);
}
private _reattachInjectors(imperativelyCreatedInjector: Injector): void {
// Dynamically-loaded component in the template. Not a root ElementInjector.
if (isPresent(this._parent)) {
@ -613,7 +624,7 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
return null;
}
throw new NoBindingError(dirDep.key);
throw new NoBindingError(null, dirDep.key);
}
return this._preBuiltObjects.templateRef;
}

View File

@ -200,6 +200,11 @@ export class AppView implements ChangeDispatcher, RenderEventDispatcher {
return isPresent(viewIndex) ? this.views[viewIndex] : null;
}
getHostElement(): ElementRef {
var boundElementIndex = this.mainMergeMapping.hostElementIndicesByViewIndex[this.viewOffset];
return this.elementRefs[boundElementIndex];
}
getDetectorFor(directive: DirectiveIndex): any {
var childView = this.getNestedView(this.elementOffset + directive.elementIndex);
return isPresent(childView) ? childView.changeDetector : null;