fix(element_injector): inject the containing change detector ref to directives

This commit is contained in:
vsavkin 2015-07-15 13:25:17 -07:00
parent f7dfd2325d
commit 7879761a41
2 changed files with 30 additions and 6 deletions

View File

@ -589,8 +589,10 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
var key: Key = dep.key; var key: Key = dep.key;
if (!(dep instanceof DirectiveDependency)) return undefinedValue; if (!(dep instanceof DirectiveDependency)) return undefinedValue;
if (!(binding instanceof DirectiveBinding)) return undefinedValue;
var dirDep = <DirectiveDependency>dep; var dirDep = <DirectiveDependency>dep;
var dirBin = <DirectiveBinding>binding;
var staticKeys = StaticKeys.instance(); var staticKeys = StaticKeys.instance();
@ -601,8 +603,12 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
if (isPresent(dirDep.queryDecorator)) return this._findQuery(dirDep.queryDecorator).list; if (isPresent(dirDep.queryDecorator)) return this._findQuery(dirDep.queryDecorator).list;
if (dirDep.key.id === StaticKeys.instance().changeDetectorRefId) { if (dirDep.key.id === StaticKeys.instance().changeDetectorRefId) {
var componentView = this._preBuiltObjects.view.componentChildViews[this._proto.index]; if (dirBin.metadata.type === DirectiveMetadata.COMPONENT_TYPE) {
return componentView.changeDetector.ref; var componentView = this._preBuiltObjects.view.componentChildViews[this._proto.index];
return componentView.changeDetector.ref;
} else {
return this._preBuiltObjects.view.changeDetector.ref;
}
} }
if (dirDep.key.id === StaticKeys.instance().elementRefId) { if (dirDep.key.id === StaticKeys.instance().elementRefId) {

View File

@ -209,7 +209,13 @@ class OptionallyInjectsProtoViewRef {
} }
@Injectable() @Injectable()
class NeedsChangeDetectorRef { class DirectiveNeedsChangeDetectorRef {
changeDetectorRef;
constructor(cdr: ChangeDetectorRef) { this.changeDetectorRef = cdr; }
}
@Injectable()
class ComponentNeedsChangeDetectorRef {
changeDetectorRef; changeDetectorRef;
constructor(cdr: ChangeDetectorRef) { this.changeDetectorRef = cdr; } constructor(cdr: ChangeDetectorRef) { this.changeDetectorRef = cdr; }
} }
@ -944,16 +950,28 @@ export function main() {
expect(inj.get(NeedsElementRef).elementRef).toBe(defaultPreBuiltObjects.view.elementRefs[0]); expect(inj.get(NeedsElementRef).elementRef).toBe(defaultPreBuiltObjects.view.elementRefs[0]);
}); });
it('should inject ChangeDetectorRef', () => { it("should inject ChangeDetectorRef of the component's view into the component", () => {
var cd = new DynamicChangeDetector(null, null, null, [], []); var cd = new DynamicChangeDetector(null, null, null, [], []);
var view = <any>new DummyView(); var view = <any>new DummyView();
var childView = new DummyView(); var childView = new DummyView();
childView.changeDetector = cd; childView.changeDetector = cd;
view.componentChildViews = [childView]; view.componentChildViews = [childView];
var inj = injector(ListWrapper.concat([NeedsChangeDetectorRef], extraBindings), null, false, var binding = DirectiveBinding.createFromType(ComponentNeedsChangeDetectorRef, new dirAnn.Component());
var inj = injector(ListWrapper.concat([binding], extraBindings), null, true,
new PreBuiltObjects(null, view, null)); new PreBuiltObjects(null, view, null));
expect(inj.get(NeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref); expect(inj.get(ComponentNeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref);
});
it("should inject ChangeDetectorRef of the containing component into directives", () => {
var cd = new DynamicChangeDetector(null, null, null, [], []);
var view = <any>new DummyView();
view.changeDetector =cd;
var binding = DirectiveBinding.createFromType(DirectiveNeedsChangeDetectorRef, new dirAnn.Directive());
var inj = injector(ListWrapper.concat([binding], extraBindings), null, false,
new PreBuiltObjects(null, view, null));
expect(inj.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref);
}); });
it('should inject ViewContainerRef', () => { it('should inject ViewContainerRef', () => {