cancel
Showing results for 
Search instead for 
Did you mean: 

If JSON model defined in manifest, how to know model data is loaded when needed in controller?

mii_guy
Explorer
556

I have JSONModel defined in manifest. When arrive in oninit of Page1 of app, my model is sometimes not loaded.

Line below sometimes errors as model is not loaded:

var oServlet = this.getOwnerComponent().getModel().getProperty("/Rowsets/Rowset/0/Row");

Is attachRequestCompleted needed in controller code, or is there a newer/better way to do this in SAPUI5?

Would a promise be better?

If so, can I use promise with JSONModel?

If so, how?

I have noticed a UI5 function in some online examples: oModel.metadataLoaded(true).then(...

Is "metadataLoaded" function only for OData models?

If so, is there a related JSON function other than attachRequestCompleted?

If attachRequestCompleted is needed, must I recreate the model. Example:

var oModel = new JSONModel(); // >>> This seems inefficient as model is already defined in manifest?

oModel.attachRequestCompleted(){function(){

console.log("manifest model is loaded.")

}

Manifest entries

Is "type" required in mainService?

"dataSources": {

"mainService": {

"uri": "/XMII/PropertyAccessServlet?Mode=List&content-type=text/json",

"type": "JSON"

}

},

Is "type" required in ""?

"models": {

"": {

"dataSource": "mainService",

"type": "sap.ui.model.json.JSONModel",

"preload": true

}

},

I am new to UI5 and would like to learn!

Thanks very much for any information that can be provided.

Accepted Solutions (1)

Accepted Solutions (1)

boghyon
Product and Topic Expert
Product and Topic Expert

Since UI5 1.64, JSONModel provides the API dataLoaded() that returns a promise (which is similar to the `metadataLoaded(true)` in v2.ODataModel). Take a look at https://stackoverflow.com/a/63892279/5846045.

doSomethingWith: async function (myJSONModel) {
  await myJSONModel.dataLoaded(); // since UI5 v1.64
  const data = myJSONModel.getProperty("/...");
},
mii_guy
Explorer

This code has allowed me to create a solution that is working! Thank you very much for the example and the links!

Answers (0)