0

在我的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)

哪里有问题?

4

1 回答 1

1

我的解决方案是为每个选定的元素建立一个承诺。如果所有承诺都已解决,则调用一个函数,该函数现在可以从和函数then访问已解决的对象。successerror

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 的更多信息

于 2019-08-20T09:26:38.230 回答