From 17f4c19c7acbad4171d9c1cd743e7c078c0b3941 Mon Sep 17 00:00:00 2001 From: Erik Ogenvik Date: Thu, 15 Oct 2015 13:22:45 +0200 Subject: [PATCH] (fix dialog) Make sure dialog only destroys itself. Since destruction is deferred, there's a risk of another dialog having been created by the time "destroy()" is called. We therefore have to make sure that we only remove one from the stack if it's still left in it (which would be the case if "destroy()" gets called as a result of the scope being destroyed independently, but wouldn't be the case if the dialog is destroyed as a result of calling $mdDialog.hide()). --- src/components/dialog/dialog.js | 2 +- .../services/interimElement/interimElement.js | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/components/dialog/dialog.js b/src/components/dialog/dialog.js index 77d668ec415..0ee0d26a3c3 100644 --- a/src/components/dialog/dialog.js +++ b/src/components/dialog/dialog.js @@ -27,7 +27,7 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) { } scope.$on('$destroy', function() { - $mdDialog.destroy(); + $mdDialog.destroyInstance(element[0].parentNode); }); /** diff --git a/src/core/services/interimElement/interimElement.js b/src/core/services/interimElement/interimElement.js index 279f425f8ab..acce78a22be 100644 --- a/src/core/services/interimElement/interimElement.js +++ b/src/core/services/interimElement/interimElement.js @@ -120,7 +120,8 @@ function InterimElementProvider() { // Special internal method to destroy an interim element without animations // used when navigation changes causes a $scope.$destroy() action - destroy : destroyInterimElement + destroy : destroyInterimElement, + destroyInstance : detroyInterimElementInstance }; @@ -222,6 +223,10 @@ function InterimElementProvider() { return interimElementService.destroy(opts); } + function detroyInterimElementInstance(opts) { + return interimElementService.destroyInstance(opts); + } + /** * Helper to call $injector.invoke with a local of the factory name for * this provider. @@ -262,7 +267,8 @@ function InterimElementProvider() { show: show, hide: hide, cancel: cancel, - destroy : destroy + destroy : destroy, + destroyInstance: destroyInstance }; /* @@ -378,6 +384,27 @@ function InterimElementProvider() { $q.when(SHOW_CANCELLED); } + /* + * Special method to quick-remove a specific interim element without animations + * + * @param element A DOM element, to which the interim element instance we want + * to destroy is attached. + */ + function destroyInstance(element) { + + if (element) { + //Try to find the interim element in the stack which corresponds to the supplied DOM element. + //This function might be called when the element already has been removed, in which + //case we won't find any matches. That's ok. + var filtered = stack.filter(function(entry) { return entry.options.element[0] === element;}); + if (filtered.length > 0) { + var interim = filtered[0]; + stack.splice(stack.indexOf(interim), 1); + return interim.remove(SHOW_CANCELLED, false, {'$destroy':true}); + } + } + return $q.when(SHOW_CANCELLED); + } /* * Internal Interim Element Object