Skip to content

Commit

Permalink
fix(viewcontroller): onDidDismiss() is always called
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Dec 2, 2016
1 parent 730e943 commit c800cc4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/components/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 @@ -239,7 +242,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
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
2 changes: 1 addition & 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
21 changes: 13 additions & 8 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;
});
return this._nav.removeView(this, options).then(() => data);
}

/**
Expand Down Expand Up @@ -489,9 +487,13 @@ 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);
}
}

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

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

/**
Expand Down

0 comments on commit c800cc4

Please sign in to comment.