The sap.ui.model.odata.v2.ODataModel
provides many useful methods to make things easier.hasPendingChanges
checks if there are pending changes in the model.
The method is very good for checking in advance whether the submitChanges
or resetChanges
method needs to be called.
The method getPendingChanges
returns the changed properties of all changed entities in a map which are still pending.
But what if we just want to submit changes of an specific entity cause we are working with setChangeGroups
?
Below is a code snippet showing how to check whether there are pending changes for a specific entityset and how to return the specific pending entity keys.
Function
/** * @description Checks whether there are pending changes for a specific entityset * @param {string} sEntitySet - name of the entityset * @returns {boolean} are there pending changes for the entityset Yes/No */ _hasPendingChangesByEntitySet: function(sEntitySet) { var oServiceModel = this.getOwnerComponent().getModel(), oPendingChanges = oServiceModel.getPendingChanges(), bChanges = false; for (var sProperty in oPendingChanges) { if (sProperty.includes(sEntitySet)) { bChanges = true; break; } } return bChanges; }, /** * @description Checks whether are pending changes and returns the entity keys * @param {string} sEntitySet - name of the entityset * @returns {array} pending entity keys */ _getPendingChangesByEntitySet: function(sEntitySet) { var oServiceModel = this.getOwnerComponent().getModel(), oPendingChanges = oServiceModel.getPendingChanges(), aPath = []; for (var sProperty in oPendingChanges) { if (sProperty.includes(sEntitySet)) { aPath.push("/" + sProperty); } } return aPath; }
Example
//reset all changes of the EntitySet "MyEntitySet" this.getOwnerComponent().getModel().resetChanges(this._getPendingChangesByEntitySet("MyEntitySet"), true);