fix(tabs): fire leaving life cycle events

Closes #2869
This commit is contained in:
Adam Bradley
2015-02-16 14:40:23 -06:00
parent 0e04f39105
commit 9cc61ecdce
3 changed files with 108 additions and 0 deletions

View File

@@ -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

View File

@@ -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);
}
}
},

View File

@@ -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('<ion-nav-view name="root"></ion-nav-view>')(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('<ion-nav-view name="root"></ion-nav-view>')(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('<div><ion-nav-view></ion-nav-view></div>')(scope));