From 082f30e60d7ac7b8d0bc4e354418168171a8855b Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Wed, 11 Mar 2015 13:54:05 -0500 Subject: [PATCH] fix(tabs): correct tab leaving lifecycle events Closes #2869 --- js/angular/controller/navViewController.js | 20 +++++++++++++++++++- js/angular/controller/tabsController.js | 5 +++++ js/angular/directive/tabs.js | 6 +++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/js/angular/controller/navViewController.js b/js/angular/controller/navViewController.js index 76f86ba6508..df2b4ab5c77 100644 --- a/js/angular/controller/navViewController.js +++ b/js/angular/controller/navViewController.js @@ -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(); @@ -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++) { @@ -216,7 +235,6 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate, viewScope && viewScope.$broadcast('$ionicView.clearCache'); } } - }; diff --git a/js/angular/controller/tabsController.js b/js/angular/controller/tabsController.js index 1d287565df9..33d6082f70c 100644 --- a/js/angular/controller/tabsController.js +++ b/js/angular/controller/tabsController.js @@ -6,6 +6,7 @@ IonicModule function($scope, $element, $ionicHistory) { var self = this; var selectedTab = null; + var previousSelectedTab = null; var selectedTabIndex; self.tabs = []; @@ -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); @@ -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)(); diff --git a/js/angular/directive/tabs.js b/js/angular/directive/tabs.js index a00b1cf8855..544c4aaf25f 100644 --- a/js/angular/directive/tabs.js +++ b/js/angular/directive/tabs.js @@ -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); } }