Skip to content

Commit

Permalink
fix($ionicPopup): stop race condition with show then hide
Browse files Browse the repository at this point in the history
Addresses #1100.
  • Loading branch information
ajoslin committed Apr 10, 2014
1 parent eb1dee9 commit 698c93f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 6 additions & 5 deletions js/ext/angular/src/service/ionicPopup.js
Original file line number Diff line number Diff line change
@@ -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>' +
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions js/ext/angular/test/service/ionicPopup.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 698c93f

Please sign in to comment.