Sapui5 多次单击SAP UI5中的按钮
我几乎已经尽了一切努力来防止多次点击按钮导致多个订单创建/服务调用-使按钮在按下时立即禁用,使其处于忙碌状态,为dblClick编写addEventDelegate,在创建订单时设置/重置标志,等等。没有任何效果 下面是我的代码: 片段内部 POUtils.jsSapui5 多次单击SAP UI5中的按钮,sapui5,Sapui5,我几乎已经尽了一切努力来防止多次点击按钮导致多个订单创建/服务调用-使按钮在按下时立即禁用,使其处于忙碌状态,为dblClick编写addEventDelegate,在创建订单时设置/重置标志,等等。没有任何效果 下面是我的代码: 片段内部 POUtils.js “setProperty”方法将触发绑定上的一些异步更新,从而可以在按钮最终被重新设置为禁用之前多次单击该按钮 您可以简单地将当前呼叫存储在控制器中,并在其运行时阻止任何其他呼叫: onSubmit: function (oEvent)
“setProperty”方法将触发绑定上的一些异步更新,从而可以在按钮最终被重新设置为禁用之前多次单击该按钮 您可以简单地将当前呼叫存储在控制器中,并在其运行时阻止任何其他呼叫:
onSubmit: function (oEvent) {
var flag = this.getModel("order").getProperty("/Other/SaveEnabled");
// CHECK THE FLAG
if (flag && !this._callOnGoing) {
this.getModel("order").setProperty("/Other/SaveEnabled", false);
this.getModel("order").setProperty("/Other/SubmitEnabled", false);
this.source = oEvent.getSource().getText();
var that = this;
// CREATE THE FLAG
this._callOnGoing = true
POUtils.onSubmit(that, that.source);
}
}
POUtils.js
onSubmit: function (oContext, mode) {
....
/*oContext.OdataModel.create("/POSet", oContext.Data, null,
oContext.success.bind(oContext), oContext.error.bind(oContext));*/
var token = null;
$.ajax({
url: sServiceURl,
type: "GET",
async: true,
beforeSend: function (xhr) {
sap.ui.core.BusyIndicator.show(0);
xhr.setRequestHeader("X-CSRF-Token", "Fetch");
},
complete: function (xhr) {
token = xhr.getResponseHeader("X-CSRF-Token");
oContext.OdataModel.create("/OrdersSet", oContext.Data, null,
oContext.successs.bind(oContext), oContext.error.bind(oContext));
// RESET THE FLAG
delete oContext._callOnGoing
}});
// error function
error: function(){
oContext.getModel("order").setProperty("/Other/SaveEnabled", true);
oContext.getModel("order").setProperty("/Other/SubmitEnabled", true);
// RESET THE FLAG
delete oContext._callOnGoing
}
我也遇到过同样的问题,用户多次单击,set属性需要花费时间,而在ide setvisible上起作用的是setvisible。另外请注意,如果您使用的是片段,则无法直接获取id,获取id的语法略有不同(您可以使用谷歌搜索相同的内容)
希望这有帮助 我在您的代码中看到了以下几行:
element.setBusy(true);
element.setBusyIndicatorDelay(0);
这会将元素设置为忙,有电流延迟(可能为1000),然后将延迟设置为0。显然,这不会有帮助。此外,它还会在每次单击按钮时设置延迟,即使该值已设置
如果你换这些线路,它应该能工作。首先设置延迟,然后设置忙状态
更好的方法是直接在视图中设置延迟。这是从不允许双击的生产性应用程序中提取的代码:
在您的视图/片段中:
<Button busyIndicatorDelay="0"
text="Save"
type="Accept"
press="onPressSave" />
确保您的后端服务支持可重复的请求,这不再是一个问题。来自UI5应用程序的每个请求都应该有一个请求Id,服务承诺只执行一次
在SAP中设置幂等服务:谢谢@Ji aSH,我试过了,但没有停止两个post呼叫。唯一不同的是,第二次电话是在一段时间之后,在第一次电话之后。
onSubmit: function (oContext, mode) {
....
/*oContext.OdataModel.create("/POSet", oContext.Data, null,
oContext.success.bind(oContext), oContext.error.bind(oContext));*/
var token = null;
$.ajax({
url: sServiceURl,
type: "GET",
async: true,
beforeSend: function (xhr) {
sap.ui.core.BusyIndicator.show(0);
xhr.setRequestHeader("X-CSRF-Token", "Fetch");
},
complete: function (xhr) {
token = xhr.getResponseHeader("X-CSRF-Token");
oContext.OdataModel.create("/OrdersSet", oContext.Data, null,
oContext.successs.bind(oContext), oContext.error.bind(oContext));
// RESET THE FLAG
delete oContext._callOnGoing
}});
// error function
error: function(){
oContext.getModel("order").setProperty("/Other/SaveEnabled", true);
oContext.getModel("order").setProperty("/Other/SubmitEnabled", true);
// RESET THE FLAG
delete oContext._callOnGoing
}
// also disable the accept button, preventing the user not to double click.
this.getView().byId("oacceptbtn").setVisible(false);
element.setBusy(true);
element.setBusyIndicatorDelay(0);
<Button busyIndicatorDelay="0"
text="Save"
type="Accept"
press="onPressSave" />
onPressSave: function (oEvent) {
const oButton = oEvent.getSource();
oButton.setBusy(true);
// more code
oModel.create(sKey, oObject, {
success: function (oResponse) {
oButton.setBusy(false);
// more success handling code
},
error: function (oError) {
oButton.setBusy(false);
// more error handling code
}
});
}