Skip to content

Commit 878c817

Browse files
committed
fix($ionicLoading): make hideOnStateChange work if loader is delayed
Closes #3022.
1 parent 2f30786 commit 878c817

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

Diff for: js/angular/service/loading.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,28 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
111111
template: LOADING_TPL,
112112
appendTo: $ionicBody.get()
113113
})
114-
.then(function(loader) {
115-
var self = loader;
116-
117-
loader.show = function(options) {
114+
.then(function(self) {
115+
self.show = function(options) {
118116
var templatePromise = options.templateUrl ?
119117
$ionicTemplateLoader.load(options.templateUrl) :
120118
//options.content: deprecated
121119
$q.when(options.template || options.content || '');
122120

123121
self.scope = options.scope || self.scope;
124122

125-
if (!this.isShown) {
123+
if (!self.isShown) {
126124
//options.showBackdrop: deprecated
127-
this.hasBackdrop = !options.noBackdrop && options.showBackdrop !== false;
128-
if (this.hasBackdrop) {
125+
self.hasBackdrop = !options.noBackdrop && options.showBackdrop !== false;
126+
if (self.hasBackdrop) {
129127
$ionicBackdrop.retain();
130128
$ionicBackdrop.getElement().addClass('backdrop-loading');
131129
}
132130
}
133131

134132
if (options.duration) {
135-
$timeout.cancel(this.durationTimeout);
136-
this.durationTimeout = $timeout(
137-
angular.bind(this, this.hide),
133+
$timeout.cancel(self.durationTimeout);
134+
self.durationTimeout = $timeout(
135+
angular.bind(self, self.hide),
138136
+options.duration
139137
);
140138
}
@@ -165,13 +163,13 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
165163
}
166164
});
167165

168-
this.isShown = true;
166+
self.isShown = true;
169167
};
170-
loader.hide = function() {
168+
self.hide = function() {
171169

172170
deregisterBackAction();
173-
if (this.isShown) {
174-
if (this.hasBackdrop) {
171+
if (self.isShown) {
172+
if (self.hasBackdrop) {
175173
$ionicBackdrop.release();
176174
$ionicBackdrop.getElement().removeClass('backdrop-loading');
177175
}
@@ -181,11 +179,11 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
181179
!self.isShown && self.element.removeClass('visible');
182180
}, 200);
183181
}
184-
$timeout.cancel(this.durationTimeout);
185-
this.isShown = false;
182+
$timeout.cancel(self.durationTimeout);
183+
self.isShown = false;
186184
};
187185

188-
return loader;
186+
return self;
189187
});
190188
}
191189
return loaderInstance;
@@ -195,14 +193,15 @@ function($ionicLoadingConfig, $ionicBody, $ionicTemplateLoader, $ionicBackdrop,
195193
options = extend({}, $ionicLoadingConfig || {}, options || {});
196194
var delay = options.delay || options.showDelay || 0;
197195

196+
deregisterStateListener();
197+
if (options.hideOnStateChange) {
198+
deregisterStateListener = $rootScope.$on('$stateChangeSuccess', hideLoader);
199+
}
200+
198201
//If loading.show() was called previously, cancel it and show with our new options
199-
loadingShowDelay && $timeout.cancel(loadingShowDelay);
202+
$timeout.cancel(loadingShowDelay);
200203
loadingShowDelay = $timeout(noop, delay);
201-
202204
loadingShowDelay.then(getLoader).then(function(loader) {
203-
if (options.hideOnStateChange) {
204-
deregisterStateListener = $rootScope.$on('$stateChangeSuccess', hideLoader);
205-
}
206205
return loader.show(options);
207206
});
208207

Diff for: test/unit/angular/service/loading.unit.js

-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ describe('$ionicLoading service', function() {
201201
template: ''
202202
});
203203
spyOn(loader, 'hide');
204-
$timeout.flush();
205204
$rootScope.$broadcast('$stateChangeSuccess');
206205
$rootScope.$apply();
207206
expect(loader.hide).toHaveBeenCalled();
@@ -213,7 +212,6 @@ describe('$ionicLoading service', function() {
213212
template: ''
214213
});
215214
spyOn(loader, 'hide');
216-
$timeout.flush();
217215
$rootScope.$broadcast('$stateChangeSuccess');
218216
$rootScope.$apply();
219217
expect(loader.hide).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)