fix(ivy): ensure Ivy *Ref classes derive from view engine equivalents (#25775)

Various user code uses 'instanceof' to check whether a particular instance
is a TemplateRef, ElementRef, etc. Ivy needs to work with these checks.

PR Close #25775
This commit is contained in:
Alex Rickabaugh 2018-08-29 13:35:43 -07:00 committed by Igor Minar
parent 96d6b79ada
commit a9099e8f70
3 changed files with 16 additions and 9 deletions

View File

@ -591,10 +591,7 @@ export const QUERY_READ_FROM_NODE =
}) as any as QueryReadType<any>); }) as any as QueryReadType<any>);
/** A ref to a node's native element. */ /** A ref to a node's native element. */
class ElementRef implements viewEngine_ElementRef { class ElementRef extends viewEngine_ElementRef {}
readonly nativeElement: any;
constructor(nativeElement: any) { this.nativeElement = nativeElement; }
}
/** /**
* Creates a ViewContainerRef and stores it on the injector. Or, if the ViewContainerRef * Creates a ViewContainerRef and stores it on the injector. Or, if the ViewContainerRef
@ -660,12 +657,14 @@ export class NodeInjector implements Injector {
* A ref to a container that enables adding and removing views from that container * A ref to a container that enables adding and removing views from that container
* imperatively. * imperatively.
*/ */
class ViewContainerRef implements viewEngine_ViewContainerRef { class ViewContainerRef extends viewEngine_ViewContainerRef {
private _viewRefs: viewEngine_ViewRef[] = []; private _viewRefs: viewEngine_ViewRef[] = [];
constructor( constructor(
private _lContainerNode: LContainerNode, private _lContainerNode: LContainerNode,
private _hostNode: LElementNode|LElementContainerNode|LContainerNode) {} private _hostNode: LElementNode|LElementContainerNode|LContainerNode) {
super();
}
get element(): ElementRef { get element(): ElementRef {
const injector = getOrCreateNodeInjectorForNode(this._hostNode); const injector = getOrCreateNodeInjectorForNode(this._hostNode);
@ -820,10 +819,12 @@ export function getInheritedFactory<T>(type: Type<any>): (type: Type<T>) => T {
} }
} }
class TemplateRef<T> implements viewEngine_TemplateRef<T> { class TemplateRef<T> extends viewEngine_TemplateRef<T> {
constructor( constructor(
private _declarationParentView: LViewData, readonly elementRef: viewEngine_ElementRef, private _declarationParentView: LViewData, readonly elementRef: viewEngine_ElementRef,
private _tView: TView, private _renderer: Renderer3, private _queries: LQueries|null) {} private _tView: TView, private _renderer: Renderer3, private _queries: LQueries|null) {
super();
}
createEmbeddedView(context: T, containerNode?: LContainerNode, index?: number): createEmbeddedView(context: T, containerNode?: LContainerNode, index?: number):
viewEngine_EmbeddedViewRef<T> { viewEngine_EmbeddedViewRef<T> {

View File

@ -209,6 +209,9 @@
{ {
"name": "_THROW_IF_NOT_FOUND" "name": "_THROW_IF_NOT_FOUND"
}, },
{
"name": "__extends"
},
{ {
"name": "__read" "name": "__read"
}, },
@ -515,6 +518,9 @@
{ {
"name": "executePipeOnDestroys" "name": "executePipeOnDestroys"
}, },
{
"name": "extendStatics"
},
{ {
"name": "extractDirectiveDef" "name": "extractDirectiveDef"
}, },

View File

@ -804,7 +804,7 @@ describe('di', () => {
class DirectiveSameInstance { class DirectiveSameInstance {
value: boolean; value: boolean;
constructor(elementRef: ElementRef, directive: Directive) { constructor(elementRef: ElementRef, directive: Directive) {
this.value = elementRef === directive.elementRef; this.value = (elementRef === directive.elementRef) && elementRef instanceof ElementRef;
} }
static ngDirectiveDef = defineDirective({ static ngDirectiveDef = defineDirective({
type: DirectiveSameInstance, type: DirectiveSameInstance,