Reputation: 5018
I have a form in a master-details application in SAPUI5. The form elements are bound to an OData model entry. I want to add an undo
button to my form, that if the user changed the data inside of the form and he or she rues of the changes and before saving the data wants to undo the changes without refreshing
the page, could press the undo
button and reload original data from OData.
Now the question is, Is there any function that I could use and refresh the data inside of the form without connecting to server or refreshing the page?
I know how to read data from Odata model and bind it again to input fields, I want to know is there any more intelligent way or not?
Upvotes: 2
Views: 7469
Reputation: 5216
It depends on what ODataModel you are using (i.e. v1, v2 or v4). I will assume that you are using v2, because v1 is now deprecated (see documentation) and v4 should be used with two-way binding. If you are still using v1, I would suggest moving to v2.
You have three possibilities which I know of for resetting the changes:
This is the easiest way to go: the v2.ODataModel has an "updateBindings" method which can be used to reset all the changes done to your input fields. The only parameter of this method should be set to true (to ensure that the binding update is forced on the controls).
//assuming that you are in a controller
this.getView().getModel().updateBindings(true);
Using the built-in two way binding of the ODataModel, you can collect changes done to your entities via the setProperty method (i.e.this is done automatically, because bindings use this method to update property values).
When the user wants to persist the changes, you can use the submitChanges method. For resetting the changes, as correctly mentioned in the comments, you can use resetChanges.
This is a better approach in my opinion, because you will not need to construct the update call body manually anymore when doing the save. Instead, you rely on the ODataModel capabilities. Of course, this implies that you will have to adjust your code a little (changing the default binding mode or the binding mode for the specific bindings, replacing the getModel().update() call).
You can refresh the data of your OData model by using the refresh model. This will actually trigger one or more backend requests, so it will not fit your case (as you said you don't want to make any server requests).
//assuming that you are in a controller
this.getView().getModel().refresh(true, false);
Upvotes: 7