refactor(compiler): don’t use AppElements for creating component views

This commit is contained in:
Tobias Bosch
2016-11-01 09:35:03 -07:00
committed by vikerman
parent 13533d2a30
commit d1035da85c
11 changed files with 79 additions and 66 deletions

View File

@ -73,7 +73,7 @@ export class CompileElement extends CompileNode {
this.instances.set(resolveIdentifierToken(Identifiers.Injector).reference, this.injector);
this.instances.set(
resolveIdentifierToken(Identifiers.Renderer).reference, o.THIS_EXPR.prop('renderer'));
if (this.hasViewContainer || this.hasEmbeddedView || isPresent(this.component)) {
if (this.hasViewContainer || this.hasEmbeddedView) {
this._createAppElement();
}
if (this.component) {

View File

@ -40,7 +40,9 @@ export class ChangeDetectorStatusEnum {
export class ViewConstructorVars {
static viewUtils = o.variable('viewUtils');
static parentInjector = o.variable('parentInjector');
static declarationEl = o.variable('declarationEl');
static parentView = o.variable('parentView');
static parentIndex = o.variable('parentIndex');
static parentElement = o.variable('parentElement');
}
export class ViewProperties {

View File

@ -24,7 +24,7 @@ export function getPropertyInView(
var currView: CompileView = callingView;
while (currView !== definedView && isPresent(currView.declarationElement.view)) {
currView = currView.declarationElement.view;
viewProp = viewProp.prop('parent');
viewProp = viewProp.prop('parentView');
}
if (currView !== definedView) {
throw new Error(

View File

@ -233,12 +233,12 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
o.importType(resolveIdentifier(Identifiers.AppView), [o.importType(component.type)])));
this.view.viewChildren.push(compViewExpr);
compileElement.setComponentView(compViewExpr);
this.view.createMethod.addStmt(
compViewExpr
.set(o.importExpr(nestedComponentIdentifier).callFn([
ViewProperties.viewUtils, compileElement.injector, compileElement.appElement
]))
.toStmt());
this.view.createMethod.addStmt(compViewExpr
.set(o.importExpr(nestedComponentIdentifier).callFn([
ViewProperties.viewUtils, compileElement.injector,
o.THIS_EXPR, o.literal(nodeIndex), renderNode
]))
.toStmt());
}
compileElement.beforeChildren();
this._addRootNodeAndProject(compileElement);
@ -442,13 +442,16 @@ function createViewClass(
ViewConstructorVars.parentInjector.name,
o.importType(resolveIdentifier(Identifiers.Injector))),
new o.FnParam(
ViewConstructorVars.declarationEl.name,
o.importType(resolveIdentifier(Identifiers.AppElement)))
ViewConstructorVars.parentView.name,
o.importType(resolveIdentifier(Identifiers.AppView), [o.DYNAMIC_TYPE])),
new o.FnParam(ViewConstructorVars.parentIndex.name, o.NUMBER_TYPE),
new o.FnParam(ViewConstructorVars.parentElement.name, o.DYNAMIC_TYPE)
];
var superConstructorArgs = [
o.variable(view.className), renderCompTypeVar, ViewTypeEnum.fromValue(view.viewType),
ViewConstructorVars.viewUtils, ViewConstructorVars.parentInjector,
ViewConstructorVars.declarationEl,
ViewConstructorVars.parentView, ViewConstructorVars.parentIndex,
ViewConstructorVars.parentElement,
ChangeDetectorStatusEnum.fromValue(getChangeDetectionMode(view))
];
if (view.genConfig.genDebugInfo) {
@ -507,8 +510,10 @@ function createViewFactory(
ViewConstructorVars.parentInjector.name,
o.importType(resolveIdentifier(Identifiers.Injector))),
new o.FnParam(
ViewConstructorVars.declarationEl.name,
o.importType(resolveIdentifier(Identifiers.AppElement)))
ViewConstructorVars.parentView.name,
o.importType(resolveIdentifier(Identifiers.AppView), [o.DYNAMIC_TYPE])),
new o.FnParam(ViewConstructorVars.parentIndex.name, o.NUMBER_TYPE),
new o.FnParam(ViewConstructorVars.parentElement.name, o.DYNAMIC_TYPE)
];
var initRenderCompTypeStmts: any[] = [];
var templateUrlInfo: string;
@ -553,8 +558,8 @@ function generateCreateMethod(view: CompileView): o.Statement[] {
var parentRenderNodeExpr: o.Expression = o.NULL_EXPR;
var parentRenderNodeStmts: any[] = [];
if (view.viewType === ViewType.COMPONENT) {
parentRenderNodeExpr = ViewProperties.renderer.callMethod(
'createViewRoot', [o.THIS_EXPR.prop('declarationAppElement').prop('nativeElement')]);
parentRenderNodeExpr =
ViewProperties.renderer.callMethod('createViewRoot', [o.THIS_EXPR.prop('parentElement')]);
parentRenderNodeStmts =
[parentRenderNodeVar.set(parentRenderNodeExpr)
.toDeclStmt(

View File

@ -24,7 +24,7 @@ export function main() {
const definedView = createCompileView({className: 'parentView'});
const callingView = createCompileView({className: 'childView', parent: definedView});
expect(getPropertyInView(expr, callingView, definedView))
.toEqual(o.THIS_EXPR.prop('parent').prop('someProp'));
.toEqual(o.THIS_EXPR.prop('parentView').prop('someProp'));
});
it('should access a known property in a parent view with cast', () => {
@ -32,7 +32,7 @@ export function main() {
const definedView = createCompileView({className: 'parentView', fields: ['someProp']});
const callingView = createCompileView({className: 'childView', parent: definedView});
expect(getPropertyInView(expr, callingView, definedView))
.toEqual(o.THIS_EXPR.prop('parent').cast(definedView.classType).prop('someProp'));
.toEqual(o.THIS_EXPR.prop('parentView').cast(definedView.classType).prop('someProp'));
});
it('should access a known property in a parent view with cast also for property chain expressions',
@ -41,7 +41,7 @@ export function main() {
const definedView = createCompileView({className: 'parentView', fields: ['someProp']});
const callingView = createCompileView({className: 'childView', parent: definedView});
expect(getPropertyInView(expr, callingView, definedView))
.toEqual(o.THIS_EXPR.prop('parent')
.toEqual(o.THIS_EXPR.prop('parentView')
.cast(definedView.classType)
.prop('someProp')
.prop('context'));