【问题标题】:How to return back ODataModel in SAPUI5 to its original state after using setDeferredGroups?使用 setDeferredGroups 后如何将 SAPUI5 中的 ODataModel 返回到其原始状态?
【发布时间】:2018-07-26 14:57:57
【问题描述】:

我有一个使用OData V2SAPUI5 应用程序。

在删除列表中项目的应用程序的一部分中,我必须在每次调用后关闭更改集。

然后我使用下面的代码:

sGroupId = "dmsch" + new Date().getTime();
oDataModel.setDeferredGroups([sGroupId]);
for (var i = 0; i < aSelectedContexts.length; i++) {
    var sObjectPath = aSelectedContexts[i].getPath();
    this._deleteObject(sObjectPath, sGroupId, fnAllRequestCompleted, fnAllRequestFailed);
}
oDataModel.submitChanges({
    groupId: sGroupId
});

_deleteObject 函数中,我为每个请求设置了不同的changeSetId,b:

_deleteObject: function(sObjectPath, sGroupId, fnSuccessCallBackFunction, fnFailedCallBackFunction) {
    var oDataModel = this.getModel();
    var sChangeSetId = "cs" + (new Date().getTime() * (1 + Math.random()));
    oDataModel.remove(sObjectPath, {
        groupId: sGroupId,
        changeSetId: sChangeSetId,
    ......

现在,在我使用 createEntry 函数创建新条目成功删除后,它会尝试将该条目的数据发送到服务器。

问题是如何重置setDeferredGroups函数的效果。

注意:我需要使用setDeferredGroups,我确信这是每次更改都会自动将新创建的条目发送到服务器的原因。我需要将 ODataModel 的设置设置回其原始状态。

注意 2:Here 是关于 oData 版本 4 的内容,它解释了失败后的这种自动行为。

【问题讨论】:

  • 默认更改组是{ "*" : { groupId: "changes" } }——所以你可以使用oModel.setChangeGroups( { "*" : { groupId: "changes"} } )oModel.setDeferredGroups(["changes"])——这就是你想要做的吗?

标签: sapui5


【解决方案1】:

SAP 文档here - 我尝试在下面进行总结。

默认更改组是

{"*": {
        groupId: "changes"
    }
}

默认的延迟组是

 ["changes"]

您可以使用

将数据模型更改组重置为默认值
oModel.setChangeGroups({"*": {
        groupId: "changes"
    }
});
oModel.setDeferredGroups(["changes"]);

使用此默认配置,所有实体类型的所有更改都将收集在changes 组中,并被延迟(不会自动发送到服务器)。

所以oModel.setChangeGroups(...) 是更改组的定义方式,oModel.setDeferredGroups 是确定每个组是否延迟的方式

我之所以提到默认更改组和默认延迟组,是因为如果设置不正确,使用双向数据绑定时可能会出现意外行为。

例如:通过调用oModel.setChangeGroups({}) 删除默认更改组将导致对所有实体类型的所有更改不会被收集到任何更改组中,因此不会被推迟。您将看到自动发送到服务器的任何更改。

假设您有一个实体类型Employee,并且您希望将此实体类型的任何更改收集到一个组中并推迟:

var oChangeGroups = oModel.getChangeGroups();
oChangeGroups.Employee = {groupId: "employees"};
oModel.setChangeGroups(oChangeGroups);

var aDeferredGroups = oModel.getDeferredGroups();
aDeferredGroups.push("employees");
oModel.setDeferredGroups(aDeferredGroups);

现在您有两个更改组,*,ID 为 changesEmployee,ID 为 employees。对任何 Employee 实体所做的任何更改都将在 employees 组中,所有其他更改将在 changes 组中。

因此,现在员工的任何创建/删除/更新都可以与对其他实体类型的任何其他更改分开提交

 oModel.createEntry("/EmployeeSet", {
     groupId: "employees",
     properties: {
         name: "New Guy"
     }
 });

 oModel.submitChanges({groupId: "employees"});

从这一点开始,要恢复默认值并摆脱employees更改组,您可以使用我上面写的内容将所有内容重置为默认值。

【讨论】:

  • 此行不正确:oModel.setChangeGroups( {“*”: {groupId: “changes” } } )
  • 另外请将字符“改为”,WEBIDE接受。你使用的字符会导致人们在使用代码时更改它。
  • @MahdiJ.Ansari 谢谢......当我输入答案时,我正在手机上。感谢您的反馈
  • @MahdiJ.Ansari 而oModel.setChangeGroups({...}) 可能不是您在这个问题中所追求的,重要的是要了解更改组/延迟组之间的关系,以便正确使用它们并理解他们的行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多