From db83d6638e22b4db47c7883a0534439673672b67 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Fri, 12 Sep 2014 23:05:27 -0500 Subject: [PATCH] refactor(tabs): prevent unnecessary updates during destroy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the containing ionTabs directive is being destroyed, then child ionTab directives should not don't bother going through its controller’s `remove` method, which selects a new active tab as each ionTab is being destroyed. By selecting a new active tab as each tab is removed, it causes unnecessary view loads, transitions and multiple `viewState.changeHistory` events. --- js/angular/directive/tab.js | 8 +++++++- js/angular/directive/tabs.js | 15 +++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/js/angular/directive/tab.js b/js/angular/directive/tab.js index 11812af4b9..ffedc72ba5 100644 --- a/js/angular/directive/tab.js +++ b/js/angular/directive/tab.js @@ -98,7 +98,13 @@ function($rootScope, $animate, $ionicBind, $compile) { tabsCtrl.add($scope); $scope.$on('$destroy', function() { - tabsCtrl.remove($scope); + if(!$scope.$tabsDestory) { + // if the containing ionTabs directive is being destroyed + // then don't bother going through the controllers remove + // method, since remove will reset the active tab as each tab + // is being destroyed, causing unnecessary view loads and transitions + tabsCtrl.remove($scope); + } tabNavElement.isolateScope().$destroy(); tabNavElement.remove(); }); diff --git a/js/angular/directive/tabs.js b/js/angular/directive/tabs.js index c228f51eda..606a506b5f 100644 --- a/js/angular/directive/tabs.js +++ b/js/angular/directive/tabs.js @@ -50,9 +50,9 @@ IonicModule.constant('$ionicTabsConfig', { IonicModule .directive('ionTabs', [ - '$ionicViewService', - '$ionicTabsDelegate', - '$ionicTabsConfig', + '$ionicViewService', + '$ionicTabsDelegate', + '$ionicTabsConfig', function($ionicViewService, $ionicTabsDelegate, $ionicTabsConfig) { return { restrict: 'E', @@ -74,7 +74,14 @@ function($ionicViewService, $ionicTabsDelegate, $ionicTabsConfig) { tabsCtrl, $attr.delegateHandle ); - $scope.$on('$destroy', deregisterInstance); + $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 + // doesn't select the next tab as the active one, which causes unnecessary + // loading of tab views when each will eventually all go away anyway + $scope.$tabsDestory = true; + deregisterInstance(); + }); tabsCtrl.$scope = $scope; tabsCtrl.$element = $element;