feat(router): implement CandDeactivate

This commit is contained in:
vsavkin
2016-06-02 14:44:57 -07:00
parent 1914847e72
commit ab958598d7
9 changed files with 132 additions and 34 deletions

View File

@ -1,4 +1,5 @@
import { Tree, TreeNode } from './utils/tree';
import { shallowEqual } from './utils/collection';
import { UrlSegment } from './url_tree';
import { Route } from './config';
import { Params, PRIMARY_OUTLET } from './shared';
@ -34,6 +35,7 @@ export function createEmptyState(rootComponent: Type): RouterState {
const emptyQueryParams = new BehaviorSubject({});
const fragment = new BehaviorSubject("");
const activated = new ActivatedRoute(emptyUrl, emptyParams, PRIMARY_OUTLET, rootComponent, snapshot.root);
activated.snapshot = snapshot.root;
return new RouterState(new TreeNode<ActivatedRoute>(activated, []), emptyQueryParams, fragment, snapshot);
}
@ -62,12 +64,18 @@ function createEmptyStateSnapshot(rootComponent: Type): RouterStateSnapshot {
* ```
*/
export class ActivatedRoute {
/** @internal */
_futureSnapshot: ActivatedRouteSnapshot;
snapshot: ActivatedRouteSnapshot;
constructor(public urlSegments: Observable<UrlSegment[]>,
public params: Observable<Params>,
public outlet: string,
public component: Type | string,
public snapshot: ActivatedRouteSnapshot
) {}
futureSnapshot: ActivatedRouteSnapshot
) {
this._futureSnapshot = futureSnapshot;
}
}
/**
@ -123,4 +131,19 @@ export class RouterStateSnapshot extends Tree<ActivatedRouteSnapshot> {
constructor(root: TreeNode<ActivatedRouteSnapshot>, public queryParams: Params, public fragment: string | null) {
super(root);
}
}
/**
* The expectation is that the activate route is created with the right set of parameters.
* So we push new values into the observables only when they are not the initial values.
* And we detect that by checking if the snapshot field is set.
*/
export function advanceActivatedRoute(route: ActivatedRoute): void {
if (route.snapshot && !shallowEqual(route.snapshot.params, route._futureSnapshot.params)) {
route.snapshot = route._futureSnapshot;
(<any>route.urlSegments).next(route.snapshot.urlSegments);
(<any>route.params).next(route.snapshot.params);
} else {
route.snapshot = route._futureSnapshot;
}
}