Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
(fix dialog) Make sure dialog only destroys itself.
Browse files Browse the repository at this point in the history
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()).
  • Loading branch information
erikogenvik committed Oct 15, 2015
1 parent 06a724f commit 17f4c19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
}

scope.$on('$destroy', function() {
$mdDialog.destroy();
$mdDialog.destroyInstance(element[0].parentNode);
});

/**
Expand Down
31 changes: 29 additions & 2 deletions src/core/services/interimElement/interimElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -262,7 +267,8 @@ function InterimElementProvider() {
show: show,
hide: hide,
cancel: cancel,
destroy : destroy
destroy : destroy,
destroyInstance: destroyInstance
};

/*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17f4c19

Please sign in to comment.