我有一个 jquery 移动对话框,用于确认用户是否要覆盖他们上传的文件。如果用户单击是,则调用上传的回调函数,如果他们单击否,则对话框关闭并且没有任何反应。
问题是如果他们用户点击否,然后再次点击上传并接受覆盖,它会调用两次回调函数。它根据他们进入对话状态的次数来建立回调,我不知道如何处理这个问题。
这是每次用户点击“上传”时的入口点
CheckOverwriteUpload: function (boxFolderId, fileName) {
var matchFound = false;
$.each(BoxManager.Entries, function (index, value) {
var entry = value;
if (entry.name == fileName) {
matchFound = true;//Found the matching file
}
})
if (matchFound) {
//Pop the dialog to ask if they want to overwrite the file
areYouSure("Overwrite File?", "The file " + fileName + " Already exists, would you like to overwrite it?", "Yes", function (result) {
if (result == true) {
//The client wants to overwrite the file, so we upload it
BoxManager.UploadFile(boxFolderId, fileName, true);
} else {
//The client does not want to overwrite. Close the dialog
$("#sure").dialog('close');
}
//Placed here to close the dialog after the possible upload
$("#sure").dialog('close');
});
} else {
//No matches, go ahead and upload
BoxManager.UploadFile(boxFolderId, fileName, matchFound);
}
},
这是对话框功能
function areYouSure(text1, text2, button, callback) {
$("#sure .sure-1").text(text1);
$("#sure .sure-2").text(text2);
$("#sure .sure-do").text(button).on("click", function () {
callback(true);
});
$("#sure .close-do").text("No").on("click", function () {
callback(false);
});
$.mobile.changePage("#sure", 'none', false, true);
}
以防万一,这里是上传代码。它只是调用服务器上的一个方法
UploadFile: function (boxFolderId, fileName, overWrite) {
$.mobile.showPageLoadingMsg();
var etag = "";
var id = "";
$.each(BoxManager.Entries, function (index, value) {
var entry = value;
if (entry.name == fileName) {
etag = entry.etag;//hash of file used to overwrite files on box.com
id = entry.id;//unique box id for file. needed to overwrite file
}
});
DocumentVaultService.UploadFileToBox(
"<%=RootFolderGuid %>",
"<%=FolderGuid %>",
"<%=FileGuid %>",
boxFolderId,
fileName,
"<%=IsClientFolder%>",
"<%=AuthToken %>",
overWrite,
etag,
id,
function (result) {
//Success on the upload, refresh the document list and close loading symbol
BoxManager.GetBoxFolderContent(boxFolderId, BoxManager.BreadCrumbList[length - 1].folderName);
$.mobile.hidePageLoadingMsg();
},
function (result) {
$.mobile.hidePageLoadingMsg();
alert("File failed to upload");
});
}
CheckOverwriteUpload 和 UploadFile 都包含在 Boxmanager 中,就像这样
var BoxManager = {
CheckOverwriteUpload:function(){},
UploadFile:function(){}
}
我怎样才能防止这个调用多次?有没有办法在我调用对话框之前清除 javascript 缓存?有没有更好的回调结构我没有看到?