Skip to content

Instantly share code, notes, and snippets.

@davecra
Last active July 27, 2017 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davecra/0abb1aa4e88196b4073b7c9585ecc89e to your computer and use it in GitHub Desktop.
Save davecra/0abb1aa4e88196b4073b7c9585ecc89e to your computer and use it in GitHub Desktop.
Workaround to closing dialogs so you can quickly open another
/**
* Closes the currently open dialog asynchronously.
* This has an ugly workaround which is to try to set a new
* event handler on the dialog until it fails. When it failed
* we know the original dialog object was destroyed and we
* can then proceed. The issue we are working around is that
* if you call two dialogs back to back, the second one will
* likely not open at all.
* @param {Office.context.ui.dialog} dialog The dialog to be closed
* @param {function()} asyncResult The callback when close is complete
*/
function dialogCloseAsync(dialog, asyncResult){
// issue the close
dialog.close();
// and then try to add a handler
// when that fails it is closed
setTimeout(function() {
try{
dialog.addEventHandler(Office.EventType.DialogMessageReceived, function() {});
dialogCloseAsync(dialog, asyncResult);
} catch(e) {
asyncResult(); // done - closed
}
}, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment