javascript - 删除请求完成后,对话框弹出窗口内的 sap.m.MessageView 内容数据未显示
问题描述
在我的blHandleDelButtonPressed()
函数中,我正在对单个/多个选定的表行执行删除条目请求。请求完成后(在success
/之后error
)我想在 Dialog 内显示一个消息视图(sap.m.MessageView
就像这里的这个一样https://sapui5.hana.ondemand.com/#/entity/sap.m.MessageView/sample/sap .m.sample.MessageViewInsideDialog)。
问题是请求DONE的时候,弹出Message View,但是里面没有数据↓↓↓
这是示例代码:
displayMsgDialog: function(oDialog) {
oDialog.open();
},
blHandleDelButtonPressed: function(oEvent) {
var oModel = this.getOwnerComponent().getModel();
var oTable = this.getView().byId("bookingsList");
var oSelectedItems = oTable.getSelectedItems();
var aBookingRemovals = [];
oTable.setBusy(true);
oSelectedItems.forEach(function(selectedItem) {
var oBooking = selectedItem.getBindingContext();
var sPath = oBooking.getPath(),
sBookId = oBooking.getProperty("bookid"),
sPassName = oBooking.getProperty("PASSNAME"),
sCustomId = oBooking.getProperty("CUSTOMID");
oModel.remove(sPath, {
success: function(oData, response) {
aBookingRemovals.push({
type: "Success",
title: "Booking " + sBookId + " successfully removed",
subtitle: "Book ID: " + sBookId + " | Passager: " + sPassName + " | ID: " + sCustomId
});
},
error: function() {
aBookingRemovals.push({
type: "Error",
title: "Booking " + selectedItem.getBindingContext().getProperty("bookid") + " wasn't removed",
subtitle: "Book ID: " + sBookId + " | Passager: " + sPassName + " | ID: " + sCustomId
});
}
});
});
oTable.setBusy(false);
var oBookingRemovalsTpl = new sap.m.MessageItem({
type: "{type}",
title: "{title}",
subtitle: "{subtitle}"
});
var oModelBookingRemovals = new JSONModel();
oModelBookingRemovals.setData(aBookingRemovals);
this.oBookingRemovalsView = new sap.m.MessageView({
showDetailsPageHeader: false,
items: {
path: "/",
template: oBookingRemovalsTpl
}
});
this.oBookingRemovalsView.setModel(oModelBookingRemovals, "BookingRemovals");
this.oBookingRemovalsDialog = new sap.m.Dialog({
resizable: true,
content: this.oBookingRemovalsView,
state: 'Information',
beginButton: new sap.m.Button({
press: function () {
this.getParent().close();
oTable.removeSelections();
aBookingRemovals = [];
},
text: "Close"
}),
customHeader: new sap.m.Bar({
contentMiddle: [
new sap.m.Text({ text: "We tried to remove selected bookings"})
]
}),
contentHeight: "300px",
contentWidth: "500px",
verticalScrolling: false
});
// Displaying the final Message View inside Dialog
this.displayMsgDialog(this.oBookingRemovalsDialog);
}
浏览器的控制台不显示任何错误/警告。
奇怪的事情:
在我选择了 3 个随机行并执行该blHandleDelButtonPressed()
函数后,我detail.oBookingRemovalsView.getModel("BookingRemovals").getData()
在 Chrome 的控制台中调用,这给了我这个 ↓↓↓ (如您所见,来自选定行的数据被插入到BookingRemovals
模型中并绑定到window.detail.oBookingRemovalsView
)
> detail.oBookingRemovalsView.getModel("BookingRemovals").getData()
(3) [{…}, {…}, {…}, {…}, {…}]
1: {type: "Error", title: "Booking 00000001 wasn't removed", subtitle: "Book ID: 00000001 | Passager: Benjamin Waldmann | ID: 00004113"}
2: {type: "Error", title: "Booking 00000002 wasn't removed", subtitle: "Book ID: 00000002 | Passager: Adam Gutenberg | ID: 00002720"}
3: {type: "Error", title: "Booking 00000005 wasn't removed", subtitle: "Book ID: 00000005 | Passager: Juan Martin | ID: 00003740"}
length: 5
__proto__: Array(0)
但是当我打电话时detail.oBookingRemovalsView.getItems()
,我得到了一个空数组↓↓↓</p>
> detail.oBookingRemovalsView.getItems()
[]
length: 0
__proto__: Array(0)
哪里有问题?
解决方案
我的解决方案是为每个选定的元素建立一个承诺。如果所有承诺都已解决,则调用一个函数,该函数现在可以从和函数then
访问已解决的对象。success
error
blHandleDelButtonPressed: function(oEvent) {
var oModel = this.getOwnerComponent().getModel();
var oTable = this.getView().byId("bookingsList");
var aSelectedItems = oTable.getSelectedItems();
// var aBookingRemovals = [];
oTable.setBusy(true);
// map applies a function to every element of an array
// and stores the returned value in a new array
var aPromises = aSelectedItems.map(function(selectedItem) {
return new Promise(function(resolve, reject) {
var oBooking = selectedItem.getBindingContext();
var sPath = oBooking.getPath(),
sBookId = oBooking.getProperty("bookid"),
sPassName = oBooking.getProperty("PASSNAME"),
sCustomId = oBooking.getProperty("CUSTOMID");
oModel.remove(sPath, {
success: function(oData, response) {
// pass the following object to the THEN function
resolve({
type: "Success",
title: "Booking " + sBookId + " successfully removed",
subtitle: "Book ID: " + sBookId + " | Passager: " + sPassName + " | ID: " + sCustomId
});
},
error: function() {
// pass the following object to the THEN function
// when using Promise.all you don't want to use reject
// otherwise the resolved promises will get lost
resolve({
type: "Error",
title: "Booking " + selectedItem.getBindingContext().getProperty("bookid") + " wasn't removed",
subtitle: "Book ID: " + sBookId + " | Passager: " + sPassName + " | ID: " + sCustomId
});
}
});
});
});
Promise.all(aPromises).then(function(aResolvedValues) {
// aResolvedValues contains all values that have been resolved
// it should look exactly the same as aBookingRemovals
// if we got here, all calls have been finished. this is a much better place to call
oTable.setBusy(false);
// add the rest of the code here
});
阅读有关承诺的更多信息
阅读更多关于Promise.all
阅读有关Array.map 的更多信息
推荐阅读
- raspberry-pi - Raspberry Pi 卡在登录/关机循环中 - 控制台无法打开(Raspbian)
- bash - 在 Bash 中拆分 csv 文件中的列
- r - 为变量设置默认值,后跟非默认变量
- python-3.x - 用于标签关系的 Python 超图设计/引擎
- c# - 如何从与后面的代码不同的类中触发 xaml 中的事件处理程序?
- code-signing - 为自由开发者在 64 位 Windows 10 上对 Windows 内核驱动程序进行代码签名的过程是什么?
- javascript - 在将 JSON 解析为对象数组时卡在 while 循环中
- javascript - 导航到 /home 或新页面时看不到 html 页面。仅显示索引
- python - 在 CSV 中使用 Pandas
- java - Hibernate 进行 N+1 查询以在超类中初始化惰性集合