feat(router): update router to support lazy loading

This commit is contained in:
vsavkin
2016-05-03 16:45:30 -07:00
parent c0cfd3c6ed
commit 0f1465b899
5 changed files with 43 additions and 24 deletions

View File

@ -9,18 +9,18 @@ import {DEFAULT_OUTLET_NAME} from './constants';
import {reflector} from '@angular/core';
// TODO: vsavkin: recognize should take the old tree and merge it
export function recognize(componentResolver: ComponentResolver, type: Type,
export function recognize(componentResolver: ComponentResolver, rootComponent: Type,
url: UrlTree): Promise<RouteTree> {
let matched = new _MatchResult(type, [url.root], {}, rootNode(url).children, []);
let matched = new _MatchResult(rootComponent, [url.root], null, rootNode(url).children, []);
return _constructSegment(componentResolver, matched).then(roots => new RouteTree(roots[0]));
}
function _recognize(componentResolver: ComponentResolver, parentType: Type,
function _recognize(componentResolver: ComponentResolver, parentComponent: Type,
url: TreeNode<UrlSegment>): Promise<TreeNode<RouteSegment>[]> {
let metadata = _readMetadata(parentType); // should read from the factory instead
let metadata = _readMetadata(parentComponent); // should read from the factory instead
if (isBlank(metadata)) {
throw new BaseException(
`Component '${stringify(parentType)}' does not have route configuration`);
`Component '${stringify(parentComponent)}' does not have route configuration`);
}
let match;
@ -32,13 +32,13 @@ function _recognize(componentResolver: ComponentResolver, parentType: Type,
let main = _constructSegment(componentResolver, match);
let aux =
_recognizeMany(componentResolver, parentType, match.aux).then(_checkOutletNameUniqueness);
_recognizeMany(componentResolver, parentComponent, match.aux).then(_checkOutletNameUniqueness);
return PromiseWrapper.all([main, aux]).then(ListWrapper.flatten);
}
function _recognizeMany(componentResolver: ComponentResolver, parentType: Type,
function _recognizeMany(componentResolver: ComponentResolver, parentComponent: Type,
urls: TreeNode<UrlSegment>[]): Promise<TreeNode<RouteSegment>[]> {
let recognized = urls.map(u => _recognize(componentResolver, parentType, u));
let recognized = urls.map(u => _recognize(componentResolver, parentComponent, u));
return PromiseWrapper.all(recognized).then(ListWrapper.flatten);
}
@ -52,23 +52,23 @@ function _constructSegment(componentResolver: ComponentResolver,
matched.consumedUrlSegments[0].outlet;
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, urlOutlet,
matched.component, factory);
factory.componentType, factory);
if (matched.leftOverUrl.length > 0) {
return _recognizeMany(componentResolver, matched.component, matched.leftOverUrl)
return _recognizeMany(componentResolver, factory.componentType, matched.leftOverUrl)
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
} else {
return _recognizeLeftOvers(componentResolver, matched.component)
return _recognizeLeftOvers(componentResolver, factory.componentType)
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
}
});
}
function _recognizeLeftOvers(componentResolver: ComponentResolver,
parentType: Type): Promise<TreeNode<RouteSegment>[]> {
return componentResolver.resolveComponent(parentType)
parentComponent: Type): Promise<TreeNode<RouteSegment>[]> {
return componentResolver.resolveComponent(parentComponent)
.then(factory => {
let metadata = _readMetadata(parentType);
let metadata = _readMetadata(factory.componentType);
if (isBlank(metadata)) {
return [];
}
@ -165,7 +165,7 @@ function _checkOutletNameUniqueness(nodes: TreeNode<RouteSegment>[]): TreeNode<R
}
class _MatchResult {
constructor(public component: Type, public consumedUrlSegments: UrlSegment[],
constructor(public component: Type|string, public consumedUrlSegments: UrlSegment[],
public parameters: {[key: string]: string},
public leftOverUrl: TreeNode<UrlSegment>[], public aux: TreeNode<UrlSegment>[]) {}
}