Skip to content

Commit

Permalink
fix(modal): fix race conditions and memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Apr 13, 2015
1 parent fdca73a commit 008df7b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions js/angular/service/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ IonicModule
'$timeout',
'$ionicPlatform',
'$ionicTemplateLoader',
'$q',
'$$q',
'$log',
'$ionicClickBlock',
function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $q, $log, $ionicClickBlock) {
'$window',
function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $$q, $log, $ionicClickBlock, $window) {

/**
* @ngdoc controller
Expand Down Expand Up @@ -122,15 +123,16 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl

if (self.scope.$$destroyed) {
$log.error('Cannot call ' + self.viewType + '.show() after remove(). Please create a new ' + self.viewType + ' instance.');
return;
return $$q.when();
}

var modalEl = jqLite(self.modalEl);

self.el.classList.remove('hide');
$timeout(function() {
if (!self._isShown) return;
$ionicBody.addClass(self.viewType + '-open');
}, 400);
}, 400, false);

if (!self.el.parentElement) {
modalEl.addClass(self.animation);
Expand All @@ -145,12 +147,14 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
if (target && self.positionView) {
self.positionView(target, modalEl);
// set up a listener for in case the window size changes
ionic.on('resize',function() {
ionic.off('resize',null,window);
self.positionView(target,modalEl);
},window);

self._onWindowResize = function(ev) {
if (self._isShown) self.positionView(target,modalEl);
};
$window.addEventListener('resize', self._onWindowResize);
}


modalEl.addClass('ng-enter active')
.removeClass('ng-leave ng-leave-active');

Expand All @@ -163,6 +167,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
ionic.views.Modal.prototype.show.call(self);

$timeout(function() {
if (!self._isShown) return;
modalEl.addClass('ng-enter-active');
ionic.trigger('resize');
self.scope.$parent && self.scope.$parent.$broadcast(self.viewType + '.shown', self);
Expand All @@ -171,6 +176,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
}, 20);

return $timeout(function() {
if (!self._isShown) return;
//After animating in, allow hide on backdrop click
self.$el.on('click', function(e) {
if (self.backdropClickToClose && e.target === self.el) {
Expand Down Expand Up @@ -198,9 +204,10 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
modalEl.addClass('ng-leave');

$timeout(function() {
if (self._isShown) return;
modalEl.addClass('ng-leave-active')
.removeClass('ng-enter ng-enter-active active');
}, 20);
}, 20, false);

self.$el.off('click');
self._isShown = false;
Expand All @@ -211,7 +218,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl

// clean up event listeners
if (self.positionView) {
ionic.off('resize',null,window);
$window.addEventListener('resize', self._onWindowResize);
}

return $timeout(function() {
Expand Down

0 comments on commit 008df7b

Please sign in to comment.