Skip to content

Commit

Permalink
fix(loading): options.hideOnStateChange: also hide on stateChangeError
Browse files Browse the repository at this point in the history
Closes #3051.
  • Loading branch information
ajoslin committed Feb 26, 2015
1 parent 864b46a commit 3d12853
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions js/angular/service/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
var loaderInstance;
//default values
var deregisterBackAction = noop;
var deregisterStateListener = noop;
var deregisterStateListener1 = noop;
var deregisterStateListener2 = noop;
var loadingShowDelay = $q.when();

return {
Expand Down Expand Up @@ -193,9 +194,11 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
options = extend({}, $ionicLoadingConfig || {}, options || {});
var delay = options.delay || options.showDelay || 0;

deregisterStateListener();
deregisterStateListener1();
deregisterStateListener2();
if (options.hideOnStateChange) {
deregisterStateListener = $rootScope.$on('$stateChangeSuccess', hideLoader);
deregisterStateListener1 = $rootScope.$on('$stateChangeSuccess', hideLoader);
deregisterStateListener2 = $rootScope.$on('$stateChangeError', hideLoader);
}

//If loading.show() was called previously, cancel it and show with our new options
Expand All @@ -219,7 +222,8 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
}

function hideLoader() {
deregisterStateListener();
deregisterStateListener1();
deregisterStateListener2();
$timeout.cancel(loadingShowDelay);
getLoader().then(function(loader) {
loader.hide();
Expand Down
14 changes: 13 additions & 1 deletion test/unit/angular/service/loading.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('$ionicLoading service', function() {
}));
});

it('should use options.hideOnStateChange', inject(function($ionicLoading, $rootScope, $timeout) {
it('should use options.hideOnStateChange to hide on $stateChangeSuccess', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
hideOnStateChange: true,
Expand All @@ -206,6 +206,18 @@ describe('$ionicLoading service', function() {
expect(loader.hide).toHaveBeenCalled();
}));

it('should use options.hideOnStateChange to hide on $stateChangeError', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
hideOnStateChange: true,
template: ''
});
spyOn(loader, 'hide');
$rootScope.$broadcast('$stateChangeError');
$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({
Expand Down

0 comments on commit 3d12853

Please sign in to comment.