From 8f0b88b7e442e5ea9f94fa4ebc44ddd2ceb977e6 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 18 Feb 2016 11:05:45 -0600 Subject: [PATCH] fix(NavController): fire onPageWillUnload/DidUnload Fixes #5507 --- ionic/components/nav/nav-controller.ts | 2 +- ionic/components/nav/test/basic/index.ts | 32 +++++++++++++++++++++++- ionic/components/nav/view-controller.ts | 32 ++++++++++-------------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/ionic/components/nav/nav-controller.ts b/ionic/components/nav/nav-controller.ts index 65a7f818bf..eb06c68423 100644 --- a/ionic/components/nav/nav-controller.ts +++ b/ionic/components/nav/nav-controller.ts @@ -802,6 +802,7 @@ export class NavController extends Ion { // set that it is the init leaving view // the first view to be removed, it should init leave view.state = STATE_INIT_LEAVE; + view.willUnload(); // from the index of the leaving view, go backwards and // find the first view that is inactive so it can be the entering @@ -836,7 +837,6 @@ export class NavController extends Ion { this._views.filter(v => v.state === STATE_REMOVE).forEach(view => { view.willLeave(); view.didLeave(); - view.didUnload(); this._views.splice(this.indexOf(view), 1); view.destroy(); }); diff --git a/ionic/components/nav/test/basic/index.ts b/ionic/components/nav/test/basic/index.ts index cad35e3432..069756f8e0 100644 --- a/ionic/components/nav/test/basic/index.ts +++ b/ionic/components/nav/test/basic/index.ts @@ -271,7 +271,9 @@ class AnotherPage { constructor( private nav: NavController, private viewCtrl: ViewController - ) {} + ) { + console.log('Page, AnotherPage, constructor', this.viewCtrl.id); + } pushFullPage() { this.nav.push(FullPage); @@ -304,6 +306,34 @@ class AnotherPage { this.viewCtrl.setBackButtonText(backButtonText); ++this.bbCount; } + + onPageWillEnter() { + console.log('Page, AnotherPage, onPageWillEnter', this.viewCtrl.id); + } + + onPageDidEnter() { + console.log('Page, AnotherPage, onPageDidEnter', this.viewCtrl.id); + } + + onPageWillLeave() { + console.log('Page, AnotherPage, onPageWillLeave', this.viewCtrl.id); + } + + onPageDidLeave() { + console.log('Page, AnotherPage, onPageDidLeave', this.viewCtrl.id); + } + + onPageWillUnload() { + console.log('Page, AnotherPage, onPageWillUnload', this.viewCtrl.id); + } + + onPageDidUnload() { + console.log('Page, AnotherPage, onPageDidUnload', this.viewCtrl.id); + } + + ngOnDestroy() { + console.log('Page, AnotherPage, ngOnDestroy', this.viewCtrl.id); + } } diff --git a/ionic/components/nav/view-controller.ts b/ionic/components/nav/view-controller.ts index d2ca968d56..268d589ff1 100644 --- a/ionic/components/nav/view-controller.ts +++ b/ionic/components/nav/view-controller.ts @@ -183,23 +183,6 @@ export class ViewController { return (this.index === 0); } - /** - * @private - */ - addDestroy(destroyFn: Function) { - this._destroys.push(destroyFn); - } - - /** - * @private - */ - destroy() { - for (var i = 0; i < this._destroys.length; i++) { - this._destroys[i](); - } - this._destroys = []; - } - /** * @private */ @@ -489,10 +472,21 @@ export class ViewController { /** * @private - * The view has been destroyed and its elements have been removed. */ - didUnload() { + addDestroy(destroyFn: Function) { + this._destroys.push(destroyFn); + } + + /** + * @private + */ + destroy() { ctrlFn(this, 'onPageDidUnload'); + + for (var i = 0; i < this._destroys.length; i++) { + this._destroys[i](); + } + this._destroys = []; } }