Skip to content

Commit

Permalink
fix(viewcontroller): onDidDismiss() is always called
Browse files Browse the repository at this point in the history
fixes #8223
  • Loading branch information
manucorporat authored and adamdbradley committed Dec 5, 2016
1 parent 66bbd24 commit 68eb1b7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/components/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class App {
// During developement, navPop can be triggered by calling
// window.ClickBackButton();
if (!window['HWBackButton']) {
window['HWBackButton'] = this.goBack.bind(this);
window['HWBackButton'] = () => {
let p = this.goBack();
p && p.catch(() => console.debug('hardware go back cancelled'));
};
}
});
}
Expand Down Expand Up @@ -233,7 +236,7 @@ export class App {
return this._menuCtrl.close();
}

let navPromise = this.navPop();
const navPromise = this.navPop();
if (navPromise === null) {
// no views to go back to
// let's exit the app
Expand Down
47 changes: 26 additions & 21 deletions src/components/nav/test/worst-case/app-module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Component, NgModule } from '@angular/core';
import { IonicApp, IonicModule, NavController, NavParams } from '../../../..';
import { DomSanitizer } from '@angular/platform-browser';


let LOG = '';
function log(message: string) {
console.log(message);
LOG += message;
function log(page: string, message: string, color: string) {
console.log(`${page}: ${message}`);
LOG += `${page}:<span style="background:${color};">${message}</span>`;
LOG += '\n';
}

Expand All @@ -25,32 +27,32 @@ const TEMPLATE: string = `
export class Base {
constructor(public _name: string) { }
ionViewWillLoad() {
log(`${this._name} willLoad`);
log(this._name, 'willLoad', 'green');
}
ionViewDidLoad() {
log(`${this._name} didLoad`);
log(this._name, 'didLoad', 'green');
}
ionViewWillEnter() {
log(`${this._name} willEnter`);
log(this._name, 'willEnter', 'greenyellow');
}
ionViewDidEnter() {
log(`${this._name} didEnter`);
log(this._name, 'didEnter', 'cyan');
}
ionViewWillLeave() {
log(`${this._name} willLeave`);
log(this._name, 'willLeave', 'greenyellow');
}
ionViewDidLeave() {
log(`${this._name} didLeave`);
log(this._name, 'didLeave', 'cyan');
}
ionViewWillUnload() {
log(`${this._name} willUnload`);
log(this._name, 'willUnload', 'lightgray');
}
ionViewCanLeave(): boolean|Promise<any> {
log(`${this._name} canLeave`);
log(this._name, 'canLeave', 'deeppink');
return true;
}
ionViewCanEnter(): boolean|Promise<any> {
log(`${this._name} canEnter`);
log(this._name, 'canEnter', '#ff78c1');
return true;
}
}
Expand All @@ -76,9 +78,9 @@ export class Page2 extends Base {
ionViewWillEnter() {
super.ionViewWillEnter();
if (this.counter > 0) {
this.nav.push(Page3);
this.nav.push(Page3, { animated: (this.counter !== 2)});
} else if (this.counter === 0) {
this.nav.push(Page4);
this.nav.push(Page4, {animate: false});
} else {
throw new Error('should not be here!');
}
Expand All @@ -90,12 +92,15 @@ export class Page2 extends Base {
template: TEMPLATE
})
export class Page3 extends Base {
constructor(private nav: NavController) {
animated: boolean;
constructor(private nav: NavController, params: NavParams) {
super('Page3');
this.animated = params.get('animated');
}

ionViewDidEnter() {
super.ionViewWillEnter();
this.nav.pop();
super.ionViewDidEnter();
this.nav.pop({animate: this.animated});
}
}

Expand Down Expand Up @@ -262,16 +267,16 @@ export class Page8 extends Base {
</ion-navbar>
</ion-header>
<ion-content padding>
<pre style="font-size: 0.72em; column-count: 3;">{{result}}</pre>
<pre style="font-size: 0.72em; column-count: 3;" [innerHTML]="result"></pre>
</ion-content>
`
})
export class Results {
result: string = 'Loading...';
constructor(private nav: NavController) {
result: any;
constructor(private nav: NavController, private sanitizer: DomSanitizer) {
}
ionViewDidEnter() {
this.result = LOG;
this.result = this.sanitizer.bypassSecurityTrustHtml(LOG);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class Tabs extends Ion implements AfterViewInit {
let deselectedPage: ViewController;
if (deselectedTab) {
deselectedPage = deselectedTab.getActive();
deselectedPage && deselectedPage._willLeave();
deselectedPage && deselectedPage._willLeave(false);
}

opts.animate = false;
Expand Down
13 changes: 8 additions & 5 deletions src/navigation/nav-controller-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ export class NavControllerBase extends Ion implements NavController {
const leavingView = this.getActive();
const enteringView = this._getEnteringView(ti, leavingView);

assert(leavingView || enteringView, 'both leavingView and enteringView are null');
if (!leavingView && !enteringView) {
ti.reject('leavingView and enteringView are null. stack is already empty');
return false;
}

// set that this nav is actively transitioning
this.setTransitioning(true);
Expand Down Expand Up @@ -423,7 +426,7 @@ export class NavControllerBase extends Ion implements NavController {
this._zone.run(() => {
for (i = 0; i < destroyQueue.length; i++) {
view = destroyQueue[i];
this._willLeave(view);
this._willLeave(view, true);
this._didLeave(view);
this._willUnload(view);
}
Expand Down Expand Up @@ -682,7 +685,7 @@ export class NavControllerBase extends Ion implements NavController {
if (enteringView || leavingView) {
this._zone.run(() => {
// Here, the order is important. WillLeave must called before WillEnter.
leavingView && this._willLeave(leavingView);
leavingView && this._willLeave(leavingView, !enteringView);
enteringView && this._willEnter(enteringView);
});
}
Expand Down Expand Up @@ -856,11 +859,11 @@ export class NavControllerBase extends Ion implements NavController {
this._app.viewDidEnter.emit(view);
}

_willLeave(view: ViewController) {
_willLeave(view: ViewController, willUnload: boolean) {
assert(this.isTransitioning(), 'nav controller should be transitioning');
assert(NgZone.isInAngularZone(), 'callback should be zoned');

view._willLeave();
view._willLeave(willUnload);
this.viewWillLeave.emit(view);
this._app.viewWillLeave.emit(view);
}
Expand Down
3 changes: 2 additions & 1 deletion src/navigation/test/view-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('ViewController', () => {
});

// act
viewController._willLeave();
viewController._willLeave(false);
}, 10000);
});

Expand Down Expand Up @@ -92,6 +92,7 @@ describe('ViewController', () => {
// arrange
let viewController = mockView();
let navControllerBase = mockNavController();
navControllerBase._isPortal = true;
mockViews(navControllerBase, [viewController]);

viewController.onWillDismiss((data: any) => {
Expand Down
28 changes: 18 additions & 10 deletions src/navigation/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class ViewController {
private _nb: Navbar;
private _onDidDismiss: Function;
private _onWillDismiss: Function;
private _dismissData: any;
private _dismissRole: any;
private _detached: boolean;

/**
Expand Down Expand Up @@ -164,20 +166,16 @@ export class ViewController {
* @param {any} [role ]
* @param {NavOptions} NavOptions Options for the dismiss navigation.
* @returns {any} data Returns the data passed in, if any.
*
*/
dismiss(data?: any, role?: any, navOptions: NavOptions = {}): Promise<any> {
if (!this._nav) {
return Promise.resolve(false);
}
this._dismissData = data;
this._dismissRole = role;

let options = assign({}, this._leavingOpts, navOptions);
this._onWillDismiss && this._onWillDismiss(data, role);
return this._nav.removeView(this, options).then(() => {
this._onDidDismiss && this._onDidDismiss(data, role);
this._onDidDismiss = null;
return data;
});
const options = assign({}, this._leavingOpts, navOptions);
return this._nav.removeView(this, options).then(() => data);
}

/**
Expand Down Expand Up @@ -489,9 +487,14 @@ export class ViewController {
* @private
* The view has is about to leave and no longer be the active view.
*/
_willLeave() {
_willLeave(willUnload: boolean) {
this.willLeave.emit(null);
this._lifecycle('WillLeave');

if (willUnload && this._onWillDismiss) {
this._onWillDismiss(this._dismissData, this._dismissRole);
this._onWillDismiss = null;
}
}

/**
Expand All @@ -517,6 +520,11 @@ export class ViewController {
_willUnload() {
this.willUnload.emit(null);
this._lifecycle('WillUnload');

this._onDidDismiss && this._onDidDismiss(this._dismissData, this._dismissRole);
this._onDidDismiss = null;
this._dismissData = null;
this._dismissRole = null;
}

/**
Expand All @@ -537,7 +545,7 @@ export class ViewController {
this._cmp.destroy();
}

this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._hdrDir = this._ftrDir = this._nb = this._onWillDismiss = null;
this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._hdrDir = this._ftrDir = this._nb = this._onDidDismiss = this._onWillDismiss = null;
}

/**
Expand Down

0 comments on commit 68eb1b7

Please sign in to comment.