1
votes

I'm using OData v4 to load data from my backend to my frontend (developed with SAP UI5) and I am using a form to display a detail page. When I click the "edit" button I'm able to edit the data. My implementation is similar to this example: https://sapui5.hana.ondemand.com/explored.html#/sample/sap.ui.layout.sample.Form354/code/Page.controller.js

When editing something, the data is directly edited at the model and, therefore, updated at the backend. However, I want to be able to choose if I want to save the changes or if I want to cancel the edit before it is updated at the backend.

I read on other questions that one can copy the ODataModel to a JSONModel and use that copy instead, by doing something like:

        var oModel = this.getView().getModel();
        var oModelJson = new sap.ui.model.json.JSONModel();
        oModel.read("/Data", {
        success: function(oData, response) {
            oModelJson.setData(oData);
            sap.ui.getCore().setModel(oModelJson, "oJSONModel");
            alert("Success!");
        },
        error: function(response) {
            alert("Error");
        }
    });

However, the read method seems not to be available for OData v4. the code of my controller where the data is loaded looks like following:

onInit: function() {
    this.oModel = new ODataModel({
        groupId : "$direct",
        synchronizationMode : "None",
        serviceUrl : '/odata/'
    });

    this.getView().setModel(this.oModel, 'oModel');

    var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
    oRouter.getRoute("details").attachPatternMatched(this._onObjectMatched, this);

    this._showFormFragment("display");

},

_onObjectMatched: function (oEvent) {
    this.getView().bindElement({
        path: "/Data(" + oEvent.getParameter("arguments").dataPath + ")",
        model: "oModel"
    });
    //I want to copy the data from the ODataModel to the JSONModel here
},

What's the best way to accomplish this? And how to do it with OData v4?

1

1 Answers

1
votes

I suppose you want to resetChanges in case user cancels the save.

For V2 ODataModel, there is deferedGroup concept which you can use to resetChanges or submitChanges.

I have not much experience with V4. Though from the documentation, it is possible.

Please try to pass a updateGroupId in the constructor. Then you can choose resetChanges or submitBatch by group Id.

mParameters.updateGroupId? The group ID that is used for update requests. If no update group ID is specified, mParameters.groupId is used. Valid update group IDs are undefined, '$auto', '$direct' or an application group ID, which is a non-empty string consisting of alphanumeric characters from the basic Latin alphabet, including the underscore.

Thank you!