feat($ionicLoading): allow options.hideOnStateChange

Closes #2676
This commit is contained in:
Andrew
2014-12-05 12:39:19 -07:00
parent 6edebbb689
commit 8d7c8903bb
2 changed files with 37 additions and 5 deletions

View File

@@ -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();

View File

@@ -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) {