From 8d7c8903bb08787490970dd6b9b51ff8477ef2e3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 5 Dec 2014 12:39:19 -0700 Subject: [PATCH] feat($ionicLoading): allow options.hideOnStateChange Closes #2676 --- js/angular/service/loading.js | 14 ++++++++---- test/unit/angular/service/loading.unit.js | 28 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/js/angular/service/loading.js b/js/angular/service/loading.js index 4d771ed522..2650195b2a 100644 --- a/js/angular/service/loading.js +++ b/js/angular/service/loading.js @@ -66,11 +66,13 @@ IonicModule '$log', '$compile', '$ionicPlatform', -function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, $timeout, $q, $log, $compile, $ionicPlatform) { + '$rootScope', +function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, $timeout, $q, $log, $compile, $ionicPlatform, $rootScope) { var loaderInstance; //default values var deregisterBackAction = angular.noop; + var deregisterStateListener = angular.noop; var loadingShowDelay = $q.when(); return { @@ -84,6 +86,8 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, * - `{string=}` `templateUrl` The url of an html template to load as the content of the indicator. * - `{object=}` `scope` The scope to be a child of. Default: creates a child of $rootScope. * - `{boolean=}` `noBackdrop` Whether to hide the backdrop. By default it will be shown. + * - `{boolean=}` `hideOnStateChange` Whether to hide the loading spinner when navigating + * to a new state. Default false. * - `{number=}` `delay` How many milliseconds to delay showing the indicator. By default there is no delay. * - `{number=}` `duration` How many milliseconds to wait until automatically * hiding the indicator. By default, the indicator will be shown until `.hide()` is called. @@ -115,10 +119,8 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, $ionicTemplateLoader.load(options.templateUrl) : //options.content: deprecated $q.when(options.template || options.content || ''); - - - self.scope = options.scope || self.scope; + self.scope = options.scope || self.scope; if (!this.isShown) { //options.showBackdrop: deprecated @@ -198,6 +200,9 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, loadingShowDelay = $timeout(angular.noop, delay); loadingShowDelay.then(getLoader).then(function(loader) { + if (options.hideOnStateChange) { + deregisterStateListener = $rootScope.$on('$stateChangeSuccess', hideLoader); + } return loader.show(options); }); @@ -215,6 +220,7 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop, } function hideLoader() { + deregisterStateListener(); $timeout.cancel(loadingShowDelay); getLoader().then(function(loader) { loader.hide(); diff --git a/test/unit/angular/service/loading.unit.js b/test/unit/angular/service/loading.unit.js index edd95f0855..419d0f0c0b 100644 --- a/test/unit/angular/service/loading.unit.js +++ b/test/unit/angular/service/loading.unit.js @@ -1,4 +1,4 @@ -describe('$ionicLoading service', function() { +ddescribe('$ionicLoading service', function() { beforeEach(module('ionic', function($provide) { //Set default options to blank for the sake of tests $provide.constant('$ionicLoadingConfig', {}); @@ -193,6 +193,32 @@ describe('$ionicLoading service', function() { expect(deregisterSpy).toHaveBeenCalled(); })); }); + + it('should use options.hideOnStateChange', inject(function($ionicLoading, $rootScope, $timeout) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + $ionicLoading.show({ + hideOnStateChange: true, + template: '' + }); + spyOn(loader, 'hide'); + $timeout.flush(); + $rootScope.$broadcast('$stateChangeSuccess'); + $rootScope.$apply(); + expect(loader.hide).toHaveBeenCalled(); + })); + + it('should default false options.hideOnStateChange', inject(function($ionicLoading, $rootScope, $timeout) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + $ionicLoading.show({ + template: '' + }); + spyOn(loader, 'hide'); + $timeout.flush(); + $rootScope.$broadcast('$stateChangeSuccess'); + $rootScope.$apply(); + expect(loader.hide).not.toHaveBeenCalled(); + })); + }); describe('$ionicLoadingConfig', function() { beforeEach(module('ionic', function($provide) {