From 9cc61ecdced8d33a138cdd5f6cdbd8f90e33484a Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Mon, 16 Feb 2015 14:40:23 -0600 Subject: [PATCH] fix(tabs): fire leaving life cycle events Closes #2869 --- js/angular/directive/tabs.js | 12 +++ js/angular/service/viewSwitcher.js | 6 ++ test/unit/angular/directive/navView.unit.js | 90 +++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/js/angular/directive/tabs.js b/js/angular/directive/tabs.js index 3622161f57a..04e19b7320c 100644 --- a/js/angular/directive/tabs.js +++ b/js/angular/directive/tabs.js @@ -82,6 +82,18 @@ function($ionicTabsDelegate, $ionicConfig, $ionicHistory) { $scope.$hasTabsTop = isTabsTop && !isHidden; }); + function emitLifecycleEvent(ev, data) { + ev.stopPropagation(); + var selectedTab = tabsCtrl.selectedTab(); + if (selectedTab) { + selectedTab.$emit(ev.name.replace('NavView', 'View'), data); + } + } + + $scope.$on('$ionicNavView.beforeLeave', emitLifecycleEvent); + $scope.$on('$ionicNavView.afterLeave', emitLifecycleEvent); + $scope.$on('$ionicNavView.leave', emitLifecycleEvent); + $scope.$on('$destroy', function() { // variable to inform child tabs that they're all being blown away // used so that while destorying an individual tab, each one diff --git a/js/angular/service/viewSwitcher.js b/js/angular/service/viewSwitcher.js index 6ee378a7681..0629459e7c9 100644 --- a/js/angular/service/viewSwitcher.js +++ b/js/angular/service/viewSwitcher.js @@ -315,6 +315,12 @@ function($timeout, $document, $q, $ionicClickBlock, $ionicConfig, $ionicNavBarDe scope.$emit('$ionicView.leave', leavingData); } } + + } else if (scope && leavingData && leavingData.viewId) { + scope.$emit('$ionicNavView.' + step + 'Leave', leavingData); + if (step == 'after') { + scope.$emit('$ionicNavView.leave', leavingData); + } } }, diff --git a/test/unit/angular/directive/navView.unit.js b/test/unit/angular/directive/navView.unit.js index d734f548eba..fdf21ae4291 100644 --- a/test/unit/angular/directive/navView.unit.js +++ b/test/unit/angular/directive/navView.unit.js @@ -920,6 +920,96 @@ describe('Ionic nav-view', function() { expect(leave.transitionId).toEqual(4); })); + it('should emit tab leaving events', inject(function ($state, $q, $timeout, $compile, $ionicConfig) { + var beforeLeave, afterLeave, leave; + scope.$on('$ionicView.beforeLeave', function(ev, d){ + beforeLeave = d; + }); + scope.$on('$ionicView.afterLeave', function(ev, d){ + afterLeave = d; + }); + scope.$on('$ionicView.leave', function(ev, d){ + leave = d; + }); + + elem.append($compile('')(scope)); + + $state.go(tab1page1State); + $q.flush(); + $timeout.flush(); + + $state.go(tab1page2State); + $q.flush(); + $timeout.flush(); + + expect(beforeLeave.stateName).toEqual('tabAbstract.tab1page1'); + expect(afterLeave.stateName).toEqual('tabAbstract.tab1page1'); + expect(leave.stateName).toEqual('tabAbstract.tab1page1'); + + $state.go(tab2page1State); + $q.flush(); + $timeout.flush(); + + expect(beforeLeave.stateName).toEqual('tabAbstract.tab1page2'); + expect(afterLeave.stateName).toEqual('tabAbstract.tab1page2'); + expect(leave.stateName).toEqual('tabAbstract.tab1page2'); + + $state.go(tab3page1State); + $q.flush(); + $timeout.flush(); + + expect(beforeLeave.stateName).toEqual('tabAbstract.tab2page1'); + expect(afterLeave.stateName).toEqual('tabAbstract.tab2page1'); + expect(leave.stateName).toEqual('tabAbstract.tab2page1'); + + $state.go(tab1page1State); + $q.flush(); + $timeout.flush(); + + expect(beforeLeave.stateName).toEqual('tabAbstract.tab3page1'); + expect(afterLeave.stateName).toEqual('tabAbstract.tab3page1'); + expect(leave.stateName).toEqual('tabAbstract.tab3page1'); + })); + + it('should emit tab $ionicView.unloaded event', inject(function ($state, $q, $timeout, $compile, $ionicConfig) { + $ionicConfig.views.maxCache(0); + + var unloadedEvent; + scope.$on('$ionicView.unloaded', function(ev, d){ + unloadedEvent = d; + }); + + elem.append($compile('')(scope)); + + $state.go(tab1page1State); + $q.flush(); + $timeout.flush(); + + $state.go(tab1page2State); + $q.flush(); + $timeout.flush(); + + expect(unloadedEvent.stateName).toEqual('tabAbstract.tab1page1'); + + $state.go(tab2page1State); + $q.flush(); + $timeout.flush(); + + expect(unloadedEvent.stateName).toEqual('tabAbstract.tab1page2'); + + $state.go(tab3page1State); + $q.flush(); + $timeout.flush(); + + expect(unloadedEvent.stateName).toEqual('tabAbstract.tab2page1'); + + $state.go(tab1page1State); + $q.flush(); + $timeout.flush(); + + expect(unloadedEvent.stateName).toEqual('tabAbstract.tab3page1'); + })); + it('should clear ion-nav-view cache', inject(function ($state, $q, $timeout, $compile, $ionicHistory) { elem.append($compile('
')(scope));