我最终使用了递归函数而不是 for 循环。
需要注意的重要一点是,当从模式(在本例中为Bootbox对话框)收到回复时,将运行一个回调函数,该函数再次调用递归函数,索引加一。这样,递归函数一直运行,直到遍历完列表中的所有项目,每次都等待响应,并且表现得像一个异步 for 循环。
这是它的样子,以防它帮助有类似问题的人。这里有一些额外的东西供我提示,但我决定将所有内容留在上下文中。
//Recursive function that behaves like a for loop, stepping through all items and finding ones needing approval.
function recursiveCheckRequests(i) {
var item = items.getItemAtIndex(i);
//Is the item we're looking at in need of approval?
if (item.get_item("_ModerationStatus") === 2) {
//Yes, prompt for approval. (code for prompt goes here.)
//Fetch info about the request to be displayed in the dialog box.
var title = item.get_item('Title');
var location = item.get_item('Location');
var description = item.get_item('Description');
var requester = item.get_item('Requester').get_lookupValue();
var start = item.get_item('EventDate').toString("h:mm tt, dddd, MMMM d, yyyy"); //formatting the datetime
var end = item.get_item('EndDate').toString("h:mm tt, dddd, MMMM d, yyyy"); //formatting the datetime
//Generate a nicely formatted message using this crazy-looking string of HTML
var html="<p>We've found a pending request from a person you manage.</p>"
html +="<p>Would you like to approve this request? </p>"
html +="<br/>"
html +="<div class=\"form-horizontal\"><!-- Start of form !-->"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">Requester</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p style=\"text-align:left\" class=\"form-control-static\">" + requester + "</p>"
html +=" </div>"
html +=" </div>"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">Title</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p type=\"text-align:left\" class=\"form-control-static\">" + title + "</p>"
html +=" </div>"
html +=" </div>"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">Location</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p type=\"text-align:left\" class=\"form-control-static\">" + location + "</p>"
html +=" </div>"
html +=" </div>"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">Start</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p type=\"text-align:left\" class=\"form-control-static\">" + start + "</p>"
html +=" </div>"
html +=" </div>"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">End</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p type=\"text-align:left\" class=\"form-control-static\">" + end + "</p>"
html +=" </div>"
html +=" </div>"
html +=" <div class=\"form-group\">"
html +=" <label class=\"col-sm-4 control-label\">Description</label>"
html +=" <div class=\"col-sm-7\">"
html +=" <p type=\"text-align:left\" class=\"form-control-static\">" + description + "</p>"
html +=" </div>"
html +=" </div>"
html +="</div> <!-- End of Form !-->"
bootbox.dialog({
message: html,
title: "A pending request needs your verdict!",
buttons: {
approve: {
label: "Approve",
className: "btn-success",
callback: function() {
//Set it to approved.
item.set_item("_ModerationStatus", 0);
item.update();
currentContext.load(item);
currentContext.executeQueryAsync(
function(){
bootbox.dialog({
title: "Request Approved.",
message: "This request has been approved.",
buttons: {
sucess:{
label: "Ok",
callback: callback
}
}
});
},
function(sender, args){
bootbox.dialog({
title: "Something went wrong!",
message: "We failed to approve the request! Here's what we know about the problem: " + args.get_message() + '\n' + args.get_stackTrace(),
buttons: {
sucess:{
label: "Ok",
callback: callback
}
}
});
});
function callback(){
//Go on to next in list when we've received a reponse.
if (i + 1 < items.get_count()) {
recursiveCheckRequests(i + 1);
}
}
}
},
reject: {
label: "Reject",
className: "btn-danger",
callback: function() {
//Set it to rejected.
item.set_item("_ModerationStatus", 1);
item.update();
currentContext.load(item);
currentContext.executeQueryAsync(
function(){
bootbox.dialog({
title: "Request Rejected.",
message: "This request has been rejected.",
buttons: {
sucess:{
label: "Ok",
callback: callback
}
}
});
},
function(sender, args){
bootbox.dialog({
title: "Something went wrong!",
message: "We failed to reject the request! Here's what we know about the problem: " + args.get_message() + '\n' + args.get_stackTrace(),
buttons: {
sucess:{
label: "Ok",
callback: callback
}
}
});
});
function callback(){
//Go on to next in list when we've received a reponse.
if (i + 1 < items.get_count()) {
recursiveCheckRequests(i + 1);
}
}
}
},
Main: {
label: "Skip",
className: "btn-default",
callback: function() {
//Leave it as pending, which is equivalent to the line below
//item.set_item("_ModerationStatus", 2);
//Go on to next in list since we don't require a reponse.
if (i + 1 < items.get_count()) {
recursiveCheckRequests(i + 1);
}
}
}
}
});
}
else
{
//bootbox.alert("This request isn't pending.");
//Go on to next in list since this one doesn't need a response.
if (i + 1 < items.get_count()) {
recursiveCheckRequests(i + 1);
}
}
}
//Start the recursive function seen above.
recursiveCheckRequests(0);