在 outlook JS 加载项中调用 REST API 时确认事件完成
Confirm event completion whilst invoking REST API in an outlook JS add-in
我正在构建一个 JS 加载项,只需单击功能区中的按钮即可将带有添加评论 'STOP' 的消息转发到定义的地址。为了转发,我用 POST
请求调用 REST API。
在函数末尾添加 event.completed()
时,使用 (event)
作为参数,getCallbackTokenAsync 根本不会 运行。
相反,如果没有它,加载项会正确转发消息,但信息栏不会消失并且脚本会保持 运行ning 循环:
信息栏表明脚本是 运行ning:https://i.stack.imgur.com/zGa2K.png
关于调用 REST API 时我应该如何正确处理 event.completed()
的任何想法?
// the function sendAsStops is called directly from the manifest file
function sendAsStops(event) {
console.log("Initialising STOP command.");
var restHost = Office.context.mailbox.restUrl;
var itemId = getItemRestId();
console.log(itemId);
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
console.log('Sending email...');
var accessToken = result.value;
console.log(result);
var getMessageUrl = restHost + '/v2.0/me/messages/' + itemId + '/forward';
$.ajax({
url: getMessageUrl,
type: 'post',
headers: { 'Authorization': 'Bearer ' + accessToken },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Comment': 'STOP',
'ToRecipients': [{'EmailAddress': { 'Address': 'name@address.com' }}],
})
}).done(function() {
console.log("Stop successfully forwarded.");
}).fail(function(error) {
console.log("Failed to send");
});
} else {
console.log("Unable to proceed. Ref: " + result.status);
return false;
}
});
event.completed();
}
function getItemRestId() {
console.log("Getting item ID...");
if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {
console.log("ID ready to use.");
return Office.context.mailbox.item.itemId;
} else {
console.log('Converting ID...');
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}
event.completed()
必须在您的 add-in 执行结束时调用。调用 event.completed()
会终止您的 add-in,因此任何异步调用(例如本例中的 getCallbackTokenAsync
回调)都会自动终止并清除。因此,您没有看到 add-in 转发邮件。[=15=]
在整个 add-in 执行完成后调用 event.completed()
。类似于:
$.ajax({
url: getMessageUrl,
type: 'post',
headers: { 'Authorization': 'Bearer ' + accessToken },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Comment': 'STOP',
'ToRecipients': [{'EmailAddress': { 'Address': 'name@address.com' }}],
})
}).done(function() {
console.log("Stop successfully forwarded.");
event.completed();
}).fail(function(error) {
console.log("Failed to send");
event.completed();
});
我正在构建一个 JS 加载项,只需单击功能区中的按钮即可将带有添加评论 'STOP' 的消息转发到定义的地址。为了转发,我用 POST
请求调用 REST API。
在函数末尾添加 event.completed()
时,使用 (event)
作为参数,getCallbackTokenAsync 根本不会 运行。
相反,如果没有它,加载项会正确转发消息,但信息栏不会消失并且脚本会保持 运行ning 循环:
信息栏表明脚本是 运行ning:https://i.stack.imgur.com/zGa2K.png
关于调用 REST API 时我应该如何正确处理 event.completed()
的任何想法?
// the function sendAsStops is called directly from the manifest file
function sendAsStops(event) {
console.log("Initialising STOP command.");
var restHost = Office.context.mailbox.restUrl;
var itemId = getItemRestId();
console.log(itemId);
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
console.log('Sending email...');
var accessToken = result.value;
console.log(result);
var getMessageUrl = restHost + '/v2.0/me/messages/' + itemId + '/forward';
$.ajax({
url: getMessageUrl,
type: 'post',
headers: { 'Authorization': 'Bearer ' + accessToken },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Comment': 'STOP',
'ToRecipients': [{'EmailAddress': { 'Address': 'name@address.com' }}],
})
}).done(function() {
console.log("Stop successfully forwarded.");
}).fail(function(error) {
console.log("Failed to send");
});
} else {
console.log("Unable to proceed. Ref: " + result.status);
return false;
}
});
event.completed();
}
function getItemRestId() {
console.log("Getting item ID...");
if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {
console.log("ID ready to use.");
return Office.context.mailbox.item.itemId;
} else {
console.log('Converting ID...');
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}
event.completed()
必须在您的 add-in 执行结束时调用。调用 event.completed()
会终止您的 add-in,因此任何异步调用(例如本例中的 getCallbackTokenAsync
回调)都会自动终止并清除。因此,您没有看到 add-in 转发邮件。[=15=]
在整个 add-in 执行完成后调用 event.completed()
。类似于:
$.ajax({
url: getMessageUrl,
type: 'post',
headers: { 'Authorization': 'Bearer ' + accessToken },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Comment': 'STOP',
'ToRecipients': [{'EmailAddress': { 'Address': 'name@address.com' }}],
})
}).done(function() {
console.log("Stop successfully forwarded.");
event.completed();
}).fail(function(error) {
console.log("Failed to send");
event.completed();
});