Skip to content

Commit

Permalink
fix(tabs): correct tab leaving lifecycle events
Browse files Browse the repository at this point in the history
Closes #2869
  • Loading branch information
adamdbradley committed Mar 11, 2015
1 parent 540124d commit 082f30e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
20 changes: 19 additions & 1 deletion js/angular/controller/navViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
$scope.$on('$ionicTabs.top', onTabsTop);
$scope.$on('$ionicSubheader', onBarSubheader);

$scope.$on('$ionicTabs.beforeLeave', onTabsLeave);
$scope.$on('$ionicTabs.afterLeave', onTabsLeave);
$scope.$on('$ionicTabs.leave', onTabsLeave);

ionic.Platform.ready(function() {
if (ionic.Platform.isWebView() && $ionicConfig.views.swipeBackEnabled()) {
self.initSwipeBack();
Expand Down Expand Up @@ -193,6 +197,21 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
};


function onTabsLeave(ev, data) {
var viewElements = $element.children();
var viewElement, viewScope;

for (var x = 0, l = viewElements.length; x < l; x++) {
viewElement = viewElements.eq(x);
if (navViewAttr(viewElement) == VIEW_STATUS_ACTIVE) {
viewScope = viewElement.scope();
viewScope && viewScope.$emit(ev.name.replace('Tabs', 'View'), data);
break;
}
}
}


self.cacheCleanup = function() {
var viewElements = $element.children();
for (var x = 0, l = viewElements.length; x < l; x++) {
Expand All @@ -216,7 +235,6 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
viewScope && viewScope.$broadcast('$ionicView.clearCache');
}
}

};


Expand Down
5 changes: 5 additions & 0 deletions js/angular/controller/tabsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ IonicModule
function($scope, $element, $ionicHistory) {
var self = this;
var selectedTab = null;
var previousSelectedTab = null;
var selectedTabIndex;
self.tabs = [];

Expand All @@ -15,6 +16,9 @@ function($scope, $element, $ionicHistory) {
self.selectedTab = function() {
return selectedTab;
};
self.previousSelectedTab = function() {
return previousSelectedTab;
};

self.add = function(tab) {
$ionicHistory.registerHistory(tab);
Expand Down Expand Up @@ -43,6 +47,7 @@ function($scope, $element, $ionicHistory) {

self.deselect = function(tab) {
if (tab.$tabSelected) {
previousSelectedTab = selectedTab;
selectedTab = selectedTabIndex = null;
tab.$tabSelected = false;
(tab.onDeselect || noop)();
Expand Down
6 changes: 3 additions & 3 deletions js/angular/directive/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function($ionicTabsDelegate, $ionicConfig, $ionicHistory) {

function emitLifecycleEvent(ev, data) {
ev.stopPropagation();
var selectedTab = tabsCtrl.selectedTab();
if (selectedTab) {
selectedTab.$emit(ev.name.replace('NavView', 'View'), data);
var previousSelectedTab = tabsCtrl.previousSelectedTab();
if (previousSelectedTab) {
previousSelectedTab.$broadcast(ev.name.replace('NavView', 'Tabs'), data);
}
}

Expand Down

0 comments on commit 082f30e

Please sign in to comment.