How to prevent OData service call on model change

蹲街弑〆低调 提交于 2019-12-02 18:36:15

问题


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.

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.

Here is my code -

Controller

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


回答1:


Your approach won't work with a ODataModel as it is strictly server side. Please use the corresponding remove method to delete an entity from the server.




回答2:


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
    }    
}


来源:https://stackoverflow.com/questions/41666601/how-to-prevent-odata-service-call-on-model-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!