odata - SAPUI5:删除后合并而不是发布
问题描述
我正在测试一个简单的示例 CRUD 应用程序。一切正常...直到我执行 DELETE (omodel.remove()) 操作。如果我做的下一个操作是插入(创建条目、绑定以查看和提交更改),则应用程序将执行 MERGE(使用已删除记录的数据)而不是 POST 并失败。之后一切都会正常工作,直到我重复这个 DELETE-INSERT 序列。如果在删除之后,尝试更新现有记录,一切都会正常进行。在开头添加 omodel.refresh() 不起作用。有任何想法吗?
准备添加第二条记录,所有模型都很好,一个现有条目和一个新条目
准备删除第一条记录,model.remove() 操作之前的情况,2 条记录,第一个等待删除
砰!未添加记录,应用程序没有添加新条目,而是与已删除记录的数据进行了合并!
用于创建新条目并将其传递给对象页面的代码
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;
}
}
});
},
谢谢,干杯!
解决方案
这不是解决这个问题的最复杂的方法,但问题与作为键的日期有关。实体中有 3 个日期字段,其中 2 个是键。一旦我将实体日期键转换为字符串(后端仍然是 DATS),一切都开始正常工作......
推荐阅读
- python - 带有moviepy和numpy的AWS Lambda错误
- javascript - Sailsjs 独立动作查看(Sailsjs V1.0.0)
- python - Python 脚本 NameError:在 Linux CentOS 上
- java - 如何通过 build.gradle 将属性和文本文件添加到 JAR 文件
- java - 从数据库读取文件内容
- javascript - 通过前端 javascript 列出服务器上的文件
- javascript - 重新加载时删除 jQuery
- jquery - 在 jQuery 中使用 .each() 设置元素的显示
- excel - Excel VBA 将工作簿作为单独的文档发送
- javascript - 将字符串格式日期从 html 发送到 javascript 函数。Javascript 只收到不同的年份