fix($ionicPopup): stop race condition with show then hide

Addresses #1100.
This commit is contained in:
Andy Joslin
2014-04-10 07:42:06 -06:00
parent eb1dee9303
commit 698c93fcaf
2 changed files with 19 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
var TPL_POPUP =
'<div class="popup">' +
'<div class="popup popup-hidden">' +
'<div class="popup-head">' +
'<h3 class="popup-title" ng-bind-html="title"></h3>' +
'<h5 class="popup-sub-title" ng-bind-html="subTitle" ng-if="subTitle"></h5>' +
@@ -320,7 +320,11 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
self.show = function() {
if (self.isShown) return;
self.isShown = true;
ionic.requestAnimationFrame(function() {
//if hidden while waiting for raf, don't show
if (!self.isShown) return;
self.element.removeClass('popup-hidden');
self.element.addClass('popup-showing active');
focusLastButton(self.element);
@@ -332,18 +336,15 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
ionic.DomUtil.centerElementByMargin(self.element[0]);
});
});
self.isShown = true;
};
self.hide = function(callback) {
callback = callback || angular.noop;
if (!self.isShown) return callback();
self.isShown = false;
self.element.removeClass('active');
self.element.addClass('popup-hidden');
$timeout(callback, 250);
self.isShown = false;
};
self.remove = function() {
if (self.removed) return;

View File

@@ -95,6 +95,19 @@ describe('$ionicPopup service', function() {
popup.show();
expect(popup.isShown).toBe(true);
});
it('should not show if isShown=false during raf wait', function() {
var popup = TestUtil.unwrapPromise($ionicPopup._createPopup());
var rafCallback;
ionic.requestAnimationFrame = function(cb) { rafCallback = cb; };
expect(popup.isShown).toBeFalsy();
popup.show();
expect(popup.isShown).toBe(true);
popup.isShown = false;
rafCallback();
expect(popup.element.hasClass('popup-showing')).toBe(false);
expect(popup.element.hasClass('active')).toBe(false);
ionic.requestAnimationFrame = function(cb) { cb(); };
});
});
describe('hide', function() {