-->

How to prevent OData service call on model change

2020-05-03 11:57发布

问题:

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


标签: odata sap sapui5