sapui5jsonmodel

JSONModel Compare Models with each other


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


Solution

  • 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