1

I only want save the data of a Model if something of the properties has changed. Therefore I would like to compare the original and the "changed" properties with each other. Found this hint by Sergio, but without a sample. https://archive.sap.com/discussions/thread/3667904

Would be nice if someone of you could help me with a nice solution.

Thanks, Dirk

fabiopagoti
  • 1,467
  • 14
  • 31
user2405095
  • 113
  • 4
  • 16

1 Answers1

1

As you are using JSONModels, extract each model's data using the getJSON method and then transform them into JS objects.

var oModelA = this.getView().getModel("modelName");
var oModelB = new sap.ui.model.json.JSONModel({
    a: 2
});
var oModelAData = JSON.parse(oModelA.getJSON());
var oModelBData = JSON.parse(oModelB.getJSON());

then, compare them using any way avaiable here that suits you

How to determine equality for two JavaScript objects?

jQuery object equality

fabiopagoti
  • 1,467
  • 14
  • 31
  • var oModelErstellerOriginalData = JSON.parse(oModelErstellerOriginal.getJSON()); var oModelErstellerData = JSON.parse(oModelErsteller.getJSON()); – user2405095 Feb 24 '18 at 13:02
  • My issue is still that there is still a reference to the changed Model. Within of the oModelErstellerOriginalData the values do have changed too. How can I avoid this reference to the changed Model? – user2405095 Feb 24 '18 at 13:05
  • `var oModelErstellerOriginalData = JSON.parse(oModelErstellerOriginal.getJSON()); this.oModelErstellerOriginal = _.clone(oModelErstellerOriginalData);` – user2405095 Feb 24 '18 at 19:59
  • 1
    Got it to work with Lodash (Removed the reference). Thanks for your help – user2405095 Feb 24 '18 at 20:00
  • `this.oModelErstellerOriginal; var oModelErstellerData = JSON.parse(oModelErsteller.getJSON()); if (_.isEqual(oModelErstellerData, this.oModelErstellerOriginal)){` For comparing of this two objects I used Lodash too. – user2405095 Feb 24 '18 at 20:59