From 1590920cdc3822c6c767a96e0cbb4bbf9b24eed6 Mon Sep 17 00:00:00 2001 From: venuatu Date: Fri, 24 Jan 2014 10:56:55 +1100 Subject: [PATCH] fix(modal): allow modal.{dismiss,close} to be called again $modalStack.openWindows.get(modalInstance) is undefined after the first dismiss and throws: - TypeError: Cannot read property 'value' of undefined Closes #1972 --- src/modal/modal.js | 8 ++++---- src/modal/test/modal.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index c753b5fc5b..082ba0b942 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -245,17 +245,17 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition']) }; $modalStack.close = function (modalInstance, result) { - var modalWindow = openedWindows.get(modalInstance).value; + var modalWindow = openedWindows.get(modalInstance); if (modalWindow) { - modalWindow.deferred.resolve(result); + modalWindow.value.deferred.resolve(result); removeModalWindow(modalInstance); } }; $modalStack.dismiss = function (modalInstance, reason) { - var modalWindow = openedWindows.get(modalInstance).value; + var modalWindow = openedWindows.get(modalInstance); if (modalWindow) { - modalWindow.deferred.reject(reason); + modalWindow.value.deferred.reject(reason); removeModalWindow(modalInstance); } }; diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 4411994087..6130af0fcf 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -108,6 +108,7 @@ describe('$modal', function () { function close(modal, result) { modal.close(result); + $timeout.flush(); $rootScope.$digest(); } @@ -135,6 +136,36 @@ describe('$modal', function () { expect($document).not.toHaveBackdrop(); }); + it('should not throw an exception on a second dismiss', function () { + + var modal = open({template: '
Content
'}); + + expect($document).toHaveModalsOpen(1); + expect($document).toHaveModalOpenWithContent('Content', 'div'); + expect($document).toHaveBackdrop(); + + dismiss(modal, 'closing in test'); + + expect($document).toHaveModalsOpen(0); + + dismiss(modal, 'closing in test'); + }); + + it('should not throw an exception on a second close', function () { + + var modal = open({template: '
Content
'}); + + expect($document).toHaveModalsOpen(1); + expect($document).toHaveModalOpenWithContent('Content', 'div'); + expect($document).toHaveBackdrop(); + + close(modal, 'closing in test'); + + expect($document).toHaveModalsOpen(0); + + close(modal, 'closing in test'); + }); + it('should open a modal from templateUrl', function () { $templateCache.put('content.html', '
URL Content
');