Skip to Content

Reset JSON model to initial values

Hi there

I'm wondering if there's an elegant solution to my problem. I need to have a "Refresh" button in my UI5 app and I don't want to reload the app. All the data are bound to local JSON models, defined in Manifest.json and the initial data are in a .json file:

Manifest.json:

...
"dataSources": {
    "Model1": {
        "uri": "model/Model1.json",
        "type": "JSON"    },
...
"models": {
    "Model1": {
        "type": "sap.ui.model.json.JSONModel",
        "dataSource": "Model1"
    },
...
Then, in my model/Model1.json file I have my initial data:

model/Model1.json:

{
    "Field1": "value1",
    "Field2": "value2"
}
When the app starts, the model works fine and the initial data is there.

In my app, "Field1" is changed to "valueA" using the setProperty() method, while "Field2" is changed to "valueB" using binding. Now, in my refreshModel() function, I want to "reset" the values of "Field1" and "Field2" to their initial values ("value1" and "value2"). I've gone through most of the methods available to sap.ui.model.json.JSONModel, but can't find a way to do it.

For the moment, I have this in my view controller:

refreshModel: function(oModel1){
oModel1.setProperty("/Field1", "value1";
oModel1.setProperty("/Field2", "value2";
}
This works fine, but I've a lot of entries and it's a bit tedious if I change the initial values or add/remove fields.

Is there a better way of "resetting" the model?

Any help appreciated,
Thomas

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Nov 02, 2018 at 02:35 PM

    Hi Thomas,

    I would add the model file in the controller definition:

    sap.ui.define([
    	"sap/ui/core/mvc/Controller",
    	"model/Model1.json"
    ], function(Controller, InitialModel) {
    

    And then reset the model with

    refreshModel: function(oModel1){
        oModel1.setData(InitialModel);
    }

    Regards,

    Marius

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 05, 2018 at 11:46 AM

    Hi Thomas, the only way is to have a "default" JSON data that allows you to replace the current data with the default one. This is the only viable way, so you should always have one default version and one updated version.

    At somepoint you will replace the updated one with the data from the default one.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 05, 2018 at 07:39 PM

    Hi Thomas,

    The simplest thing that you could do is:

    refresh: function(oModel) {
        oModel.setData([]);
    }

    It avoids the creation of several json files on your project.

    BR
    Arthur Silva

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 07, 2018 at 09:45 AM
    -1

    Thanks for getting back to me.

    Marius Obert's solution did not work for me as it expect a Javascript file.

    I ended up with the solution I tried to avoid, and guess I'll have to live with that...

    window.location.reload(true);

    Thanks for your input though!

    Regards,
    Thomas

    Add comment
    10|10000 characters needed characters exceeded