javascript - 使用异步功能时如何防止关闭引导箱?
问题描述
我想要得到的是,如果用户收到任何验证错误,那么 bootbox 将显示“这是必需的”。到现在为止,我已经做到了。但主要问题是 - 如果用户单击“是”按钮,它会关闭启动框窗口。我得到这个是因为我不得不在引导箱中使用 g异步回调。出于这个原因,即使在返回false之后,引导箱也在关闭。但我希望用户在按下取消按钮之前显示该框。如果他们单击Yes,那么它应该显示验证,并打开该框。这是我的代码:
bootbox.confirm({
message: 'Test',
buttons: {
confirm: {
label: 'Yes',
className: 'btn-primary'
},
cancel: {
label: 'No',
className: 'btn-danger'
}
},
callback: async function (result) {
var isValid = $('#form').valid();
if (result && !isValid) {
return false; //it's not working. It's closing the bootbox
}
if (result && isValid) {
/* for this await function I had to use async callback,
without this bootbox is opend
*/
var data = await self.createData();
$.ajax({
type: "POST",
success: function (result) {
},
}).then(function () {
});
}
}
});
我该如何解决这个问题?
解决方案
在我看来,它对您尝试使用bootbox.confirm()
的回调没有任何形式的支持。async
当您的回调返回时引导箱关闭,await
除非您明确返回,否则您将在第一次点击时关闭false
,但async
回调函数总是返回一个未明确返回的承诺false
。你无法改变这一点。
您可以做的是使您的回调成为常规回调函数,而不是在验证失败async
时返回false
,然后创建一个嵌入式async
函数,您可以在其中使用await
您从第一个回调中调用的函数,如下所示。请注意,引导箱将在您的异步代码完成之前关闭,因此如果引导箱代码中有任何错误,您将需要以新的方式呈现这些错误,也许是建立一个新的引导箱。这是在仍然使用await
.
bootbox.confirm({
message: 'Test',
buttons: {
confirm: {
label: 'Yes',
className: 'btn-primary'
},
cancel: {
label: 'No',
className: 'btn-danger'
}
},
callback: function (result) {
var isValid = $('#form').valid();
if (result) {
if (!isValid) {
// keep prompt open until user presses Cancel
return false;
}
async function run() {
const data = await self.createData();
const result = await $.ajax({ ... });
// do something with result
}
// now call async function here (dialog will close)
run().catch(err => {
// do something with an error here
console.log(err);
});
}
return true;
}
});
或者,您可以避免使用await
并且只使用.then()
and.catch()
然后您就不需要这个额外的功能层:
bootbox.confirm({
message: 'Test',
buttons: {
confirm: {
label: 'Yes',
className: 'btn-primary'
},
cancel: {
label: 'No',
className: 'btn-danger'
}
},
callback: function (result) {
var isValid = $('#form').valid();
if (result) {
if (!isValid) {
// keep prompt open until user presses Cancel
return false;
}
self.createData().then(data => {
return $.ajax({ ... }).then(result => {
// do something with result
});
}).catch(err => {
// do something with error here
console.log(err);
});
}
return true;
}
});
推荐阅读
- json - TD Ameritrade API 获取头寸
- python - 我可以澄清什么是“列表列表的数据结构”吗?
- python - 本地和全局执行 np.random.seed() 之间的区别/
- javascript - array.includes 的 javascript 奇怪行为
- javascript - 试图通过文本元素循环,并一一设置动画
- c++ - 我应该如何减轻出现两次的cin?
- c++ - 如何使用 C+ 从 csv 文件中导入字典
- opengl - OpenGL库是否包含不需要在Visual Studio的设置中指定的目录?
- python - 范围不连续时的 Pandas Groupby Ranges
- powershell - Powershell 导出到 csv 时间跨度总和