如何防止模型更改时的OData服务调用 [英] How to prevent OData service call on model change

查看:81
本文介绍了如何防止模型更改时的OData服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sap.m.Table,其项目"绑定到oData v2模型.我需要在删除图标上单击以删除项目.这是我的工作: 单击删除图标后,我得到了模型中的所有行,删除了有问题的行并再次设置了模型的属性. 但是,由于更改了模型,它将触发后端往返并带来最新数据,并且表再次显示原始行.

I have a sap.m.Table whose "items" are bound to oData v2 model. I need to delete item on click on delete icon. Here is what I do: On click of delete icon, I get all the rows in the model, delete the one in question and set the property of model again. However since the model is changed, it triggers a backend round trip and brings the latest data and table shows the original rows again.

我尝试将绑定模式设置为OneTime,但这不起作用.还尝试将RefreshAfterChange设置为false,但即使这样,服务还是被再次调用.

I tried setting binding mode to OneTime but that does not work. Also tried setting RefreshAfterChange to false but even then service was called again.

这是我的代码-

控制器

onInit: function() {
    var oModel = new sap.ui.model.odata.v2.ODataModel("url", {
        json: true,
        useBatch : false,
        refreshAfterChange: false,
        defaultBindingMode: "OneTime"
    });

    this.getView.().setModel(oModel, "model1");
},

onDeleteIconPress : function(oEvent) {
    // get the selected row
    // get all the rows in oOriginalRows
    // loop over oOriginalRows and delete the selected row from it

    // set the model to reformed oOriginalRows
    this.getView().getModel("omodel1").setProperty("/", oOriginalRows);
   // Till this point every thing looks fine. I can see changes in the model
    // refresh is called automatically and data service triggers backend call
    // This fetches original data again and table shows all data again
}

如何不能再次触发往返?我需要在本地更新

How can I not trigger the round trip again? I need to update the locally

推荐答案

由于Odata是服务器端模型,因此它总是触发往返.所以我没有将sap.m.Table绑定到数据模型.相反,我手动触发了读取.成功后,我将接收到的数据复制到本地JSON模型.我将表项绑定到此JSON模型.现在,删除按钮可以正常工作了.

Since Odata is server side model, it always triggered a round trip. So I did not bind my sap.m.Table to Data model. Instead I triggered a read manually. On success I copied the data received to local JSON model. I bound my table items to this JSON model. Now the delete button works just fine.

// Define a JSON Model
oJsonModel = new sap.ui.model.json.JSONModel();

//oModel is Odata model defined in manifest file
oModel.read("/entity1", {
    success: function(oData, oResponse){
        oJsonModel.setProperty("/entity1", oData.results);

        // bind oJsonModel to table here
    }    
}

这篇关于如何防止模型更改时的OData服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆