refactor(core): view engine - remove `ViewData.parentDiIndex` (#14225)

Instead, calculate it on the fly

Part of #14013
This commit is contained in:
Tobias Bosch 2017-01-31 17:42:46 -08:00 committed by Miško Hevery
parent 45e1e36477
commit ae7f5f37d2
3 changed files with 20 additions and 14 deletions

View File

@ -220,7 +220,7 @@ export function resolveDep(
if (elDef.parent != null) {
elIndex = elDef.parent;
} else {
elIndex = view.parentDiIndex;
elIndex = parentDiIndex(view);
view = view.parent;
}
}
@ -246,12 +246,26 @@ export function resolveDep(
return asProviderData(view, providerIndex).instance;
}
}
elIndex = view.parentDiIndex;
elIndex = parentDiIndex(view);
view = view.parent;
}
return Injector.NULL.get(depDef.token, notFoundValue);
}
/**
* for component views, this is the same as parentIndex.
* for embedded views, this is the index of the parent node
* that contains the view container.
*/
function parentDiIndex(view: ViewData): number {
if (view.parent) {
const parentNodeDef = view.def.nodes[view.parentIndex];
return parentNodeDef.element && parentNodeDef.element.template ? parentNodeDef.parent :
parentNodeDef.index;
}
return view.parentIndex;
}
export function createInjector(view: ViewData, elIndex: number): Injector {
return new Injector_(view, elIndex);
}

View File

@ -259,10 +259,6 @@ export interface ViewData {
// index of parent element / anchor. Not the index
// of the provider with the component view.
parentIndex: number;
// for component views, this is the same as parentIndex.
// for embedded views, this is the index of the parent node
// that contains the view container.
parentDiIndex: number;
parent: ViewData;
component: any;
context: any;

View File

@ -223,8 +223,7 @@ function cloneAndModifyElement(
export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context?: any): ViewData {
// embedded views are seen as siblings to the anchor, so we need
// to get the parent of the anchor and use it as parentIndex.
const view = createView(
parent.services, parent, anchorDef.index, anchorDef.parent, anchorDef.element.template);
const view = createView(parent.services, parent, anchorDef.index, anchorDef.element.template);
initView(view, parent.component, context);
createViewNodes(view);
return view;
@ -236,15 +235,14 @@ export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context
*/
export function createRootView(
services: Services, defFactory: ViewDefinitionFactory, context?: any): ViewData {
const view = createView(services, null, null, null, resolveViewDefinition(defFactory));
const view = createView(services, null, null, resolveViewDefinition(defFactory));
initView(view, context, context);
createViewNodes(view);
return view;
}
function createView(
services: Services, parent: ViewData, parentIndex: number, parentDiIndex: number,
def: ViewDefinition): ViewData {
services: Services, parent: ViewData, parentIndex: number, def: ViewDefinition): ViewData {
const nodes: NodeData[] = new Array(def.nodes.length);
let renderer: Renderer;
if (def.flags != null && (def.flags & ViewFlags.DirectDom)) {
@ -257,7 +255,6 @@ function createView(
def,
parent,
parentIndex,
parentDiIndex,
context: undefined,
component: undefined, nodes,
state: ViewState.FirstCheck, renderer, services,
@ -303,8 +300,7 @@ function _createViewNodes(view: ViewData) {
if (nodeDef.provider.component) {
const hostElIndex = nodeDef.parent;
componentView = createView(
view.services, view, hostElIndex, hostElIndex,
resolveViewDefinition(nodeDef.provider.component));
view.services, view, hostElIndex, resolveViewDefinition(nodeDef.provider.component));
}
const providerData = nodeData = createProvider(view, nodeDef, componentView);
if (componentView) {