v2.ODataModel.refresh(true)获取更新的数据但不更新模型。

huangapple go评论79阅读模式
英文:

v2.ODataModel.refresh(true) gets updated data but doesn't update the model

问题

我遇到了一个关于 sap.ui.model.odata.v2.ODataModel#refresh API 的奇怪问题,导致我的UI无法正确更新。

我正在使用一个多选表格,通过它,我允许两种不同类型的更新,我已经以相同的方式实现了它们

// 基础控制器
getModel(name, global = false) {
   const scope = global ? this.getOwnerComponent() : this.getView();
   return scope.getModel(name);
},

updateEntry(model, path, data, options) {
    return new Promise((resolve, reject) => {
        const parameters = {
            ...options,
            success: (data, response) => {
                resolve({ data, response });
            },
            error: error => {
                reject(error);
            },
        };

        model.update(path, data, parameters);
    });
},
// 表格控制器,用于两种情况的更新处理程序,“main”模型通过列表绑定绑定到我的表格
onUpdate: async function (oEvent) {
    const bContinue = await SubmitDialog.init.call(this);
    if (!bContinue) return;
    const srv = this.getModel("main", true);
    // ... 获取表格数据 / 一些额外的逻辑
    const aUpdates = [];
    aAssets.forEach(asset => {
        // ... 更新字段,设置URI等。
        aUpdates.push(this.updateEntry(srv, `/EntitySet${uri}`, body, { groupId: "update", refreshAfterChange: false }));
    });
    await Promise.all(aUpdates);
    srv.refresh(true);
},

最后在同一个控制器中

onAfterRendering: function () {
    this.byId("table").getBinding("items").attachDataReceived(() => {
        console.log("Model after update", this.getModel("main").getProperty("/"));
        this._initUIData(); // 一些UI更改
    });
},

更新处理程序的功能与我期望的完全相同。我首先执行一个包含所有更新的$batch请求,然后执行一个$batch GET请求,以获取更新后的数据,然后根据需要修改我的UI(例如,在Icon Tab Bar中显示计数器等)。

然而,对于其中一种情况,我可以看到,尽管在GET请求之后接收到的有效载荷反映了我的更改,但模型根本没有更新,因此我的UI更新逻辑失败。我认为这可能与我的 dataReceived 事件处理程序有关,但是我真的不确定为什么。值得一提的是,在其中一个更新处理程序(正常工作的那个)中,我在 srv.refresh(true) 之后关闭了一个对话框,但这大约是唯一不同的地方。

也值得一提的是,我在我的OData模型中使用了 Client 操作模式。

非常感谢任何提示或想法。

英文:

I am running into a weird issue with the sap.ui.model.odata.v2.ODataModel#refresh API that prevents my UI from updating properly.

I am using a Multi Select Table via which I allow two different types of Updates that I have implemented identically

// Base Controller
getModel(name, global = false) {
   const scope = global ? this.getOwnerComponent() : this.getView();
   return scope.getModel(name);
},

updateEntry(model, path, data, options) {
    return new Promise((resolve, reject) => {
        const parameters = {
            ...options,
            success: (data, response) => {
                resolve({ data, response });
            },
            error: error => {
                reject(error);
            },
        };

        model.update(path, data, parameters);
    });
},
// Table Controller, Update Handler for both scenarios, Model "main" is bound via List Binding to my Table
onUpdate: async function (oEvent) {
    const bContinue = await SubmitDialog.init.call(this);
    if (!bContinue) return;
    const srv = this.getModel("main", true);
    // ... Fetching Table Data / some additional logic
    const aUpdates = [];
    aAssets.forEach(asset => {
        // ... Updating fields setting uri etc.
        aUpdates.push(this.updateEntry(srv, `/EntitySet${uri}`, body, { groupId: "update", refreshAfterChange: false }));
    });
    await Promise.all(aUpdates);
    srv.refresh(true);
},

and finally in the same controller

onAfterRendering: function () {
    this.byId("table").getBinding("items").attachDataReceived(() => {
        console.log("Model after update", this.getModel("main").getProperty("/"));
        this._initUIData(); // some UI changes
    });
},

The Update handler functions exactly as I am expecting it to. I first do a $batch request with all my updates followed by a $batch GET request to get the updated data to then modify my UI accordingly (i.e. displaying counters in an Icon Tab Bar etc.)

However, for one of the scenarios I can see that, although the payload I am receiving after the GET request reflects my changes, the model is simply not updated, so that my logic to update the UI fails. I am assuming that it is somehow related to my dataReceived event handler, but I am honestly not sure why. FWIW in one of the update handlers (the one that is working properly), I am closing a Dialog after the srv.refresh(true) but that's about the only thing that is different.

It may also be worth mentioning that I use Client operation mode for my OData model.

Any hints or ideas are highly appreciated.

答案1

得分: 1

我相信我可能已经找到了解决方案。我的第二个更新情况(那个没有更新模型的情况)是一个更新,导致列表条目不再出现在我的表格中。我认为在这种情况下v2.ODataModel#refresh的以下条件没有满足,即使我已经将第一个参数bForceUpdate设置为true

> 这将重新加载模型中存储的所有数据。这将检查所有绑定是否有更新的数据更新控件,如果数据已更改。

我现在也将第二个参数bRemoveData设置为true,这似乎已经解决了我的问题。

英文:

I believe I may have found the solution. My second update scenario (the one that hasn't updated the model) is an update, that causes the list entries to no longer appear in my table. I believe in this case the following condition of the v2.ODataModel#refresh is not met, even though I had set the 1st parameter bForceUpdate to true.

> This will reload all data stored in the model. This will check all bindings for updated data and update the controls if data has been changed.

I have now set the 2nd parameter bRemoveData to true as well and this seems to have fixed my issue.

huangapple
  • 本文由 发表于 2023年8月4日 04:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定