Skip to content

Commit

Permalink
fix(tabs): fire leaving life cycle events
Browse files Browse the repository at this point in the history
Closes #2869
  • Loading branch information
adamdbradley committed Feb 16, 2015
1 parent 0e04f39 commit 9cc61ec
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions js/angular/directive/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions js/angular/service/viewSwitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},

Expand Down
90 changes: 90 additions & 0 deletions test/unit/angular/directive/navView.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 9cc61ec

Please sign in to comment.