From 76d6f5fa0d3cd6b376c2327e5073442e2b8c5a8d Mon Sep 17 00:00:00 2001 From: vsavkin Date: Sat, 30 Apr 2016 10:33:14 -0700 Subject: [PATCH] fix(router): canDeactivate should not change the url when returns false Closes #8360 --- modules/angular2/src/alt_router/router.ts | 15 +++++++++------ .../angular2/test/alt_router/integration_spec.ts | 4 +++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/modules/angular2/src/alt_router/router.ts b/modules/angular2/src/alt_router/router.ts index a156ab9e38..862364b60b 100644 --- a/modules/angular2/src/alt_router/router.ts +++ b/modules/angular2/src/alt_router/router.ts @@ -59,10 +59,12 @@ export class Router { .then(currTree => { return new _LoadSegments(currTree, this._prevTree) .load(this._routerOutletMap, this._rootComponent) - .then(_ => { - this._prevTree = currTree; - this._location.go(this._urlSerializer.serialize(this._urlTree)); - this._changes.emit(null); + .then(updated => { + if (updated) { + this._prevTree = currTree; + this._location.go(this._urlSerializer.serialize(this._urlTree)); + this._changes.emit(null); + } }); }); } @@ -90,7 +92,7 @@ class _LoadSegments { constructor(private currTree: Tree, private prevTree: Tree) {} - load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise { + load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise { let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null; let currRoot = rootNode(this.currTree); @@ -100,6 +102,7 @@ class _LoadSegments { if (res) { this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]); } + return res; }); } @@ -189,7 +192,7 @@ class _LoadSegments { } private unloadOutlet(outlet: RouterOutlet, components: Object[]): void { - if (outlet.isLoaded) { + if (isPresent(outlet) && outlet.isLoaded) { StringMapWrapper.forEach(outlet.outletMap._outlets, (v, k) => this.unloadOutlet(v, components)); if (this.performMutation) { diff --git a/modules/angular2/test/alt_router/integration_spec.ts b/modules/angular2/test/alt_router/integration_spec.ts index d97554161d..854768f63f 100644 --- a/modules/angular2/test/alt_router/integration_spec.ts +++ b/modules/angular2/test/alt_router/integration_spec.ts @@ -127,7 +127,7 @@ export function main() { }))); 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); router.navigateByUrl('/team/22/cannotDeactivate'); @@ -138,6 +138,8 @@ export function main() { expect(fixture.debugElement.nativeElement) .toHaveText('team 22 { cannotDeactivate, aux: }'); + + expect(location.path()).toEqual('/team/22/cannotDeactivate'); }))); if (DOM.supportsDOMEvents()) {