首页 > 解决方案 > SAPUI5:删除后合并而不是发布

问题描述

我正在测试一个简单的示例 CRUD 应用程序。一切正常...直到我执行 DELETE (omodel.remove()) 操作。如果我做的下一个操作是插入(创建条目、绑定以查看和提交更改),则应用程序将执行 MERGE(使用已删除记录的数据)而不是 POST 并失败。之后一切都会正常工作,直到我重复这个 DELETE-INSERT 序列。如果在删除之后,尝试更新现有记录,一切都会正常进行。在开头添加 omodel.refresh() 不起作用。有任何想法吗?

从空记录集开始,在提交更改之前添加第一条记录、模型 在此处输入图像描述

添加记录,执行后 在此处输入图像描述

准备添加第二条记录,所有模型都很好,一个现有条目和一个新条目 在此处输入图像描述

添加了第二条记录,另一个 POST,一切顺利 在此处输入图像描述

准备删除第一条记录,model.remove() 操作之前的情况,2 条记录,第一个等待删除 在此处输入图像描述

记录已删除,DELETE 操作按预期触发 在此处输入图像描述

最后一步,即将输入另一条记录,查看现有和待处理的新条目 在此处输入图像描述

砰!未添加记录,应用程序没有添加新条目,而是与已删除记录的数据进行了合并! 在此处输入图像描述

用于创建新条目并将其传递给对象页面的代码

        onActionAdd: function() {

        var oModel = this.getView().getModel();
        var oParamModel = this.getView().getModel("Params");
        oParamModel.setProperty("/ObjectMode", "Add");
        oModel.refresh();

        //var oNewObject = "{\"Curr\": \"GBP\"}";
        var oNewObject = "{\"Pernr\": \"1023912\",\"Begda\":\"" + new Date('2021', '05', '01').toString()
                          + "\",\"Endda\":\"" + new Date('2021', '06', '01').toString()
                          //+ "\",\"ActionDate\":\"" + new Date('2021', '07', '01').toString() 
                          + "\"}";
        oNewObject = JSON.parse(oNewObject);

        var oEntry = oModel.createEntry("/Industrial_ActionSet", {
            properties: oNewObject
        }); 

        oParamModel.setProperty("/EntryId", oEntry.sPath.toString());           
        this.getRouter().navTo("object", {
            objectId: oNewObject.Pernr,
            dateFromId: oNewObject.Begda,
            dateToId: oNewObject.Endda              
        });
    }

保存代码(插入/更新)

        onActionSave: function() {

        var oModel = this.getView().getModel();
        var oParamModel = this.getView().getModel("Params");
        var objectMode = oParamModel.getProperty("/ObjectMode");            
        var self = this;

        // abort if the  model has not been changed 
        if (!oModel.hasPendingChanges()) {
            MessageBox.information(
                this.getResourceBundle().getText("noChangesMessage"), {
                    id: "noChangesInfoMessageBox",
                    styleClass: self.getOwnerComponent().getContentDensityClass()
                }
            );
            return;
        }   
        if (objectMode === "Add") {
            var sDateFrom = new Date(this.getView().byId("idDateFrom").getDateValue());
            var sObjectPath = oParamModel.getProperty("/EntryId") + "/Begda";
            oModel.setProperty(sObjectPath, sDateFrom);     
            var sDateTo = new Date(this.getView().byId("idDateTo").getDateValue());
            sObjectPath = oParamModel.getProperty("/EntryId") + "/Endda";
            oModel.setProperty(sObjectPath, sDateTo);                   
            var sActionDate = new Date(this.getView().byId("idActionDate").getDateValue());
            sObjectPath = oParamModel.getProperty("/EntryId") + "/ActionDate";
            oModel.setProperty(sObjectPath, sActionDate);
            var sMethod = "POST";
        } else {
            sMethod = "PUT";
        }

        oModel.submitChanges({
            method: sMethod,                
            success: function(oData, sResponse) {
                MessageToast.show("Record Updated");
                self.onNavBack();
            },
            error: function(oError) {
                jQuery.sap.log.error("Action Save oData Failure", oError);
            }
        });         
    },

删除代码

        onActionDelete: function() {

        var oModel = this.getView().getModel();
        var msgText = this.getModel("i18n").getResourceBundle().getText("confirmDelete");
        var sPath = this.getView().getBindingContext().sPath;
        var self = this;

        // Opens the confirmation dialog
        MessageBox.confirm(msgText, {
            title: "Exit Confirmation",
            initialFocus: sap.m.MessageBox.Action.CANCEL,
            onClose: function(sButton) {
                if (sButton === MessageBox.Action.OK) {
                    oModel.remove(sPath, {
                        method: "DELETE",
                        success: function(data) {
                            MessageToast.show("Record Deleted");
                            self.onNavBack();                               
                        },
                        error: function(e) {
                            jQuery.sap.log.error("Delete Action oData Failure", e);
                        }
                    });
                } else if (sButton === MessageBox.Action.CANCEL) {
                    MessageToast.show("Deletion aborted");
                    return;
                }
            }
        });         
    },  

谢谢,干杯!

标签: odatasapui5crud

解决方案


这不是解决这个问题的最复杂的方法,但问题与作为键的日期有关。实体中有 3 个日期字段,其中 2 个是键。一旦我将实体日期键转换为字符串(后端仍然是 DATS),一切都开始正常工作......


推荐阅读