From 6f052d1daf216a792b613a3a4a079e062b20eb88 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 1 Jun 2016 13:30:24 -0700 Subject: [PATCH] feat(router): add a function to resolve components --- modules/@angular/router/src/resolve.ts | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 modules/@angular/router/src/resolve.ts diff --git a/modules/@angular/router/src/resolve.ts b/modules/@angular/router/src/resolve.ts new file mode 100644 index 0000000000..737145a605 --- /dev/null +++ b/modules/@angular/router/src/resolve.ts @@ -0,0 +1,28 @@ +import { RouterStateCandidate, ActivatedRouteCandidate } from './router_state'; +import { TreeNode } from './utils/tree'; +import { ComponentResolver } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/operator/map'; +import {forkJoin} from 'rxjs/observable/forkJoin'; +import {fromPromise} from 'rxjs/observable/fromPromise'; +import 'rxjs/add/operator/toPromise'; + +export function resolve(resolver: ComponentResolver, state: RouterStateCandidate): Observable { + return resolveNode(resolver, state._root).map(_ => state); +} + +function resolveNode(resolver: ComponentResolver, node: TreeNode): Observable { + if (node.children.length === 0) { + return fromPromise(resolver.resolveComponent(node.value.component).then(factory => { + node.value._resolvedComponentFactory = factory; + return node.value; + })); + + } else { + const c = node.children.map(c => resolveNode(resolver, c).toPromise()); + return forkJoin(c).map(_ => resolver.resolveComponent(node.value.component).then(factory => { + node.value._resolvedComponentFactory = factory; + return node.value; + })); + } +} \ No newline at end of file