diff --git a/js/angular/controller/listController.js b/js/angular/controller/listController.js index c0eb7b06f9..46c118f2d4 100644 --- a/js/angular/controller/listController.js +++ b/js/angular/controller/listController.js @@ -78,13 +78,18 @@ IonicModule '$scope', '$attrs', '$ionicListDelegate', -function($scope, $attrs, $ionicListDelegate) { + '$ionicHistory', +function($scope, $attrs, $ionicListDelegate, $ionicHistory) { var self = this; var isSwipeable = true; var isReorderShown = false; var isDeleteShown = false; - var deregisterInstance = $ionicListDelegate._registerInstance(self, $attrs.delegateHandle); + var deregisterInstance = $ionicListDelegate._registerInstance( + self, $attrs.delegateHandle, function() { + return $ionicHistory.isActiveScope($scope); + } + ); $scope.$on('$destroy', deregisterInstance); self.showReorder = function(show) { diff --git a/js/angular/controller/scrollController.js b/js/angular/controller/scrollController.js index 7d4cbac549..b81ac2070f 100644 --- a/js/angular/controller/scrollController.js +++ b/js/angular/controller/scrollController.js @@ -32,16 +32,7 @@ function($scope, scrollViewOptions, $timeout, $window, $location, $document, $io var deregisterInstance = $ionicScrollDelegate._registerInstance( self, scrollViewOptions.delegateHandle, function() { - if ($scope.$$disconnected) { - return false; - } - - var currentHistoryId = $ionicHistory.currentHistoryId(); - if (currentHistoryId) { - return currentHistoryId == (isDefined($scope.$historyId) ? $scope.$historyId : 'root'); - } - - return true; + return $ionicHistory.isActiveScope($scope); } ); diff --git a/js/angular/controller/sideMenuController.js b/js/angular/controller/sideMenuController.js index 9c25d97d8b..cf0fd80d96 100644 --- a/js/angular/controller/sideMenuController.js +++ b/js/angular/controller/sideMenuController.js @@ -397,7 +397,9 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io }); var deregisterInstance = $ionicSideMenuDelegate._registerInstance( - self, $attrs.delegateHandle + self, $attrs.delegateHandle, function() { + return $ionicHistory.isActiveScope($scope); + } ); $scope.$on('$destroy', function() { diff --git a/js/angular/directive/slideBox.js b/js/angular/directive/slideBox.js index f39f18c15e..f518967279 100644 --- a/js/angular/directive/slideBox.js +++ b/js/angular/directive/slideBox.js @@ -39,7 +39,8 @@ IonicModule .directive('ionSlideBox', [ '$ionicSlideBoxDelegate', '$window', -function($ionicSlideBoxDelegate, $window) { + '$ionicHistory', +function($ionicSlideBoxDelegate, $window, $ionicHistory) { return { restrict: 'E', @@ -64,7 +65,11 @@ function($ionicSlideBoxDelegate, $window) { function postLink(scope, element, attr, slideBoxCtrl) { element.addClass('slider'); - var deregister = $ionicSlideBoxDelegate._registerInstance(slideBoxCtrl, attr.delegateHandle); + var deregister = $ionicSlideBoxDelegate._registerInstance( + slideBoxCtrl, attr.delegateHandle, function() { + return $ionicHistory.isActiveScope(scope); + } + ); watchSelected(); isDefined(attr.loop) && watchLoop(); diff --git a/js/angular/directive/tabs.js b/js/angular/directive/tabs.js index 2472262613..e473e674c1 100644 --- a/js/angular/directive/tabs.js +++ b/js/angular/directive/tabs.js @@ -47,7 +47,8 @@ IonicModule .directive('ionTabs', [ '$ionicTabsDelegate', '$ionicConfig', -function($ionicTabsDelegate, $ionicConfig) { + '$ionicHistory', +function($ionicTabsDelegate, $ionicConfig, $ionicHistory) { return { restrict: 'E', scope: true, @@ -65,7 +66,9 @@ function($ionicTabsDelegate, $ionicConfig) { return { pre: prelink, post: postLink }; function prelink($scope, $element, $attr, tabsCtrl) { var deregisterInstance = $ionicTabsDelegate._registerInstance( - tabsCtrl, $attr.delegateHandle + tabsCtrl, $attr.delegateHandle, function() { + return $ionicHistory.isActiveScope($scope); + } ); tabsCtrl.$scope = $scope; diff --git a/js/angular/service/history.js b/js/angular/service/history.js index d00f594384..3cebb634b6 100644 --- a/js/angular/service/history.js +++ b/js/angular/service/history.js @@ -441,7 +441,10 @@ function($rootScope, $state, $location, $window, $ionicViewSwitcher, $ionicNavVi * @description The app's current view. * @returns {object} Returns the current view. */ - currentView: function() { + currentView: function(view) { + if (arguments.length) { + viewHistory.currentView = view; + } return viewHistory.currentView; }, @@ -636,6 +639,17 @@ function($rootScope, $state, $location, $window, $ionicViewSwitcher, $ionicNavVi isAbstractEle: function(ele) { return !!(ele && (isAbstractTag(ele) || isAbstractTag(ele.children()))); + }, + + isActiveScope: function(scope) { + if (!scope || scope.$$disconnected) return false; + + var currentHistoryId = this.currentHistoryId(); + if (currentHistoryId) { + return currentHistoryId == (isDefined(scope.$historyId) ? scope.$historyId : 'root'); + } + + return true; } }; diff --git a/test/unit/angular/controller/listController.unit.js b/test/unit/angular/controller/listController.unit.js index ffd01c5d2b..ee9b473424 100644 --- a/test/unit/angular/controller/listController.unit.js +++ b/test/unit/angular/controller/listController.unit.js @@ -17,7 +17,7 @@ describe('$ionicList controller', function() { spyOn($ionicListDelegate, '_registerInstance'); var ctrl = setup({delegateHandle: 'foobar'}); expect($ionicListDelegate._registerInstance) - .toHaveBeenCalledWith(ctrl, 'foobar'); + .toHaveBeenCalledWith(ctrl, 'foobar', jasmine.any(Function)); })); it('should register with given handle and deregister on destroy', inject(function($ionicListDelegate) { @@ -29,7 +29,7 @@ describe('$ionicList controller', function() { delegateHandle: 'something' }); expect($ionicListDelegate._registerInstance) - .toHaveBeenCalledWith(ctrl, 'something'); + .toHaveBeenCalledWith(ctrl, 'something', jasmine.any(Function)); expect(deregisterSpy).not.toHaveBeenCalled(); ctrl.$scope.$destroy(); diff --git a/test/unit/angular/directive/sideMenu.unit.js b/test/unit/angular/directive/sideMenu.unit.js index efc2b3ab2b..707eed844c 100644 --- a/test/unit/angular/directive/sideMenu.unit.js +++ b/test/unit/angular/directive/sideMenu.unit.js @@ -17,7 +17,7 @@ describe('Ionic Angular Side Menu', function() { expect(el.controller('ionSideMenus')).toBeDefined(); expect($ionicSideMenuDelegate._registerInstance) - .toHaveBeenCalledWith(el.controller('ionSideMenus'), 'superHandle'); + .toHaveBeenCalledWith(el.controller('ionSideMenus'), 'superHandle', jasmine.any(Function)); expect(deregisterSpy).not.toHaveBeenCalled(); el.scope().$destroy(); diff --git a/test/unit/angular/directive/tabs.unit.js b/test/unit/angular/directive/tabs.unit.js index 98d5c5c050..5de35573d5 100644 --- a/test/unit/angular/directive/tabs.unit.js +++ b/test/unit/angular/directive/tabs.unit.js @@ -206,7 +206,7 @@ describe('tabs', function() { var el = setup('delegate-handle="banana"'); expect($ionicTabsDelegate._registerInstance) - .toHaveBeenCalledWith(el.controller('ionTabs'), 'banana'); + .toHaveBeenCalledWith(el.controller('ionTabs'), 'banana', jasmine.any(Function)); expect(deregisterSpy).not.toHaveBeenCalled(); el.scope().$destroy(); diff --git a/test/unit/angular/service/history.unit.js b/test/unit/angular/service/history.unit.js index 71d349f84c..d3e62b1e01 100644 --- a/test/unit/angular/service/history.unit.js +++ b/test/unit/angular/service/history.unit.js @@ -1108,6 +1108,62 @@ describe('Ionic History', function() { expect(newView.stateName).toEqual('about'); })); + it('should not be active when scope null', function() { + expect(ionicHistory.isActiveScope(null)).toEqual(false); + expect(ionicHistory.isActiveScope(undefined)).toEqual(false); + expect(ionicHistory.isActiveScope()).toEqual(false); + }); + + it('should not be active when scope disconnected', function() { + var scope = { + $$disconnected: true + }; + expect(ionicHistory.isActiveScope(scope)).toEqual(false); + }); + + it('should be active w/ scope but no current history id', function() { + ionicHistory.registerHistory('1234'); + expect(ionicHistory.isActiveScope(scope)).toEqual(true); + }); + + it('should be active w/ scope same history id as current view', function() { + ionicHistory.currentView({ + historyId: '123' + }); + + var scope = { + $historyId: '123' + } + expect(ionicHistory.isActiveScope(scope)).toEqual(true); + }); + + it('should be not active w/ scope different history id as current view', function() { + ionicHistory.currentView({ + historyId: '123' + }); + + var scope = { + $historyId: 'abc' + } + expect(ionicHistory.isActiveScope(scope)).toEqual(false); + }); + + it('should be active when scope w/ unknown history and current view root history id', function() { + ionicHistory.currentView({ + historyId: 'root' + }); + + expect(ionicHistory.isActiveScope({})).toEqual(true); + }); + + it('should not be active when scope w/ unknown history and current view not root history id', function() { + ionicHistory.currentView({ + historyId: '123' + }); + + expect(ionicHistory.isActiveScope({})).toEqual(false); + }); + it('should go() to a view', inject(function($location) { var newView = ionicHistory.createView({ stateName: 'about' }); newView.go();