fix(router): canDeactivate should not change the url when returns false

Closes #8360
This commit is contained in:
vsavkin 2016-04-30 10:33:14 -07:00 committed by Victor Savkin
parent 0f1b370117
commit 76d6f5fa0d
2 changed files with 12 additions and 7 deletions

View File

@ -59,10 +59,12 @@ export class Router {
.then(currTree => { .then(currTree => {
return new _LoadSegments(currTree, this._prevTree) return new _LoadSegments(currTree, this._prevTree)
.load(this._routerOutletMap, this._rootComponent) .load(this._routerOutletMap, this._rootComponent)
.then(_ => { .then(updated => {
this._prevTree = currTree; if (updated) {
this._location.go(this._urlSerializer.serialize(this._urlTree)); this._prevTree = currTree;
this._changes.emit(null); this._location.go(this._urlSerializer.serialize(this._urlTree));
this._changes.emit(null);
}
}); });
}); });
} }
@ -90,7 +92,7 @@ class _LoadSegments {
constructor(private currTree: Tree<RouteSegment>, private prevTree: Tree<RouteSegment>) {} constructor(private currTree: Tree<RouteSegment>, private prevTree: Tree<RouteSegment>) {}
load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<void> { load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null; let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null;
let currRoot = rootNode(this.currTree); let currRoot = rootNode(this.currTree);
@ -100,6 +102,7 @@ class _LoadSegments {
if (res) { if (res) {
this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]); this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]);
} }
return res;
}); });
} }
@ -189,7 +192,7 @@ class _LoadSegments {
} }
private unloadOutlet(outlet: RouterOutlet, components: Object[]): void { private unloadOutlet(outlet: RouterOutlet, components: Object[]): void {
if (outlet.isLoaded) { if (isPresent(outlet) && outlet.isLoaded) {
StringMapWrapper.forEach(outlet.outletMap._outlets, StringMapWrapper.forEach(outlet.outletMap._outlets,
(v, k) => this.unloadOutlet(v, components)); (v, k) => this.unloadOutlet(v, components));
if (this.performMutation) { if (this.performMutation) {

View File

@ -127,7 +127,7 @@ export function main() {
}))); })));
it('should not unload the route if can deactivate returns false', it('should not unload the route if can deactivate returns false',
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
let fixture = tcb.createFakeAsync(RootCmp); let fixture = tcb.createFakeAsync(RootCmp);
router.navigateByUrl('/team/22/cannotDeactivate'); router.navigateByUrl('/team/22/cannotDeactivate');
@ -138,6 +138,8 @@ export function main() {
expect(fixture.debugElement.nativeElement) expect(fixture.debugElement.nativeElement)
.toHaveText('team 22 { cannotDeactivate, aux: }'); .toHaveText('team 22 { cannotDeactivate, aux: }');
expect(location.path()).toEqual('/team/22/cannotDeactivate');
}))); })));
if (DOM.supportsDOMEvents()) { if (DOM.supportsDOMEvents()) {