cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JSON model loading after the view is rendered, thus leaving the view blank

Amol4484
Newcomer
0 Kudos
575

I am facing a problem in sapui5 where my view is loading before the model maintained in the manifest.json is pulling data from the given json file and because of this the view is rendering blank. I have to refresh the page manually for view to wait for model to load before the view is rendered. I have used attachRequestCompleted function on the model to get model data but sometimes while navigating from one view to another I am facing this issue. I have also maintained preload: true for model in manifest. I can see the json file loading in the network tab. How can I solve this issue, does anyone have any idea?

Accepted Solutions (0)

Answers (2)

Answers (2)

Noel_Hendrikx
Active Contributor
0 Kudos

Hi Amol,

Could you share a bit of code with us to see how you try to achieve this? 

I assume you are loading the json file with the loadData function. This returns a promise (https://sapui5.hana.ondemand.com/#/api/sap.ui.model.json.JSONModel%23methods/loadData)

What you can do depends on your application. If you are using a router, you could implement an async await in your onObjectMatched function. This async awaits waits till the loadData is done and with the result (the json file) you could set it back to your jsonmodel with setData as junwu also suggests.

For the sake of simplicity I did not write error handling.

 
public async _onRouteMatched() {
    const oModel = this.getView().getModel("myjsonmodel");
    const mydata = await this.loadMyJSONData(oModel);
    oModel.setData(mydata);
}

public async loadMyJSONData(oModel) {
    const oResponse = await oModel.loadData("https://jsonplaceholder.typicode.com/todos/1");
    return oResponse.json();
}

Kind regards,

Noël

junwu
SAP Champion
SAP Champion
0 Kudos

jsonmodel.setData

or

jsonmodel.refresh(true)

either of them should make your view updated.