cancel
Showing results for 
Search instead for 
Did you mean: 

Reset JSON model to initial values

thomas_arnesen
Explorer
0 Kudos
4,864

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

View Entire Topic
thomas_arnesen
Explorer

Thanks for getting back to me.

mariusobert'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