Reputation: 13
I'm using office dialog api in word add-in. when I set a localstorage & read in the dialog after it opens, I see the value in the dialog. But when the dialog is open & I set the localstorage in parent, I do not see that showing up in the dialog. I'm using localstorage for communication to child dialog after launch. Is there any other way of communicating between parent & child after the dialog is open? using Office.context.ui.messageParent when sending message from dialog to parent.
Main page:
$scope.opensettingDialog = function () {
Office.context.ui.displayDialogAsync("https://localhost/tz/setting", {90,50},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processSettingDialog);
});}
function processSettingDialog (arg) {
var messageFromDialog = JSON.parse(arg.message);
if (messageFromDialog.messageType == "save") {
//do operations & store result
localStorage.setItem("tbSave", "true");
}
else {
dialog.close();
}
}
Settings Dialog:
$scope.acceptSettings = function () {
var messageObject = {
messageType: "savedoc"
};
var jsonMessage = JSON.stringify(messageObject);
Office.context.ui.messageParent(jsonMessage);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
};
//Wait for parent operation to complete
var checkSave2Update= function () {
var processingStatus = localStorage.getItem("tbSave");//it never has the value set in the parent page.
if ((processingStatus == "undefined") || (processingStatus == null)) {
$interval.cancel($scope.intervalpromise);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
return;
}
else {//cancel wait
$interval.cancel($scope.intervalpromise);
}
}
Upvotes: 0
Views: 697
Reputation: 8670
In terms of localStorage, it might just be that you're running into an Internet Explorer issue (on the desktop, Office Add-in run inside of an IE container).
The issue and workaround are described in detail in https://stackoverflow.com/a/40770399. Basically, local storage may get out of sync across tabs (and that's effectively what a taskpane vs. dialog are). To fix this, you can set a value on some key, and this will ensure that localStorage is refreshed.
We use this workaround in Script Lab: see https://github.com/OfficeDev/script-lab/blob/master/packages/common/src/utilities/ensure.fresh.local.storage.ts
Hope this helps.
Upvotes: 1