cancel
Showing results for 
Search instead for 
Did you mean: 

Define several OData models asynchronously

dunayevsky_yuri
Participant
0 Kudos
501

Hello,

I try to define several OData model asynchronously.

Here how I define them (all OData models declarations are grouped in a separate function):


var oODataModel = new sap.ui.model.odata.ODataModel(

  "url1", {

  loadMetadataAsync: true

  }).attachMetadataLoaded(function(){

  that.getView().setModel(oODataModel, "model1_odata");

  });


oODataModel = new sap.ui.model.odata.ODataModel(

  "url2", {

  loadMetadataAsync: true

  }).attachMetadataLoaded(function(){

  that.getView().setModel(oODataModel, "model2_odata");

  });


and so on.


The problem is that further in code I need to call to those "modelX_odata" models and do reads, while the models are still not set (async calls are still in progress).


Is there a way to wait for all those models are complete calls and are set?


Thank you,

Yuri


Accepted Solutions (0)

Answers (2)

Answers (2)

junwu
Active Contributor
0 Kudos

you can set model directly, why wait until the metadata loaded?

dunayevsky_yuri
Participant
0 Kudos

That's not the problem. The problem is that I do read() before the model is finished the declaration (before the metadata is loaded). And this returns me an error.

Is there a way to declare several OData models using deferreds?

Like,


doDeclare: function(){

var oODataModel1 = new sap.ui.model.odata.ODataModel("url1", {

  loadMetadataAsync : true

  });

  this.getView().setModel(oODataModel1, "odata1");

  var oODataModel2 = new sap.ui.model.odata.ODataModel("url2", {

  loadMetadataAsync : true

  });

  this.getView().setModel(oODataModel2, "odata2");

},

doContinue: function(){

     //do stuff here

}

And do something like:


$.when(this.doDeclare()).done(this.doContinue());

so all async calls and metadata loads in doDeclare() are finished before doContinue() starts?

If this was a regular ajax call, I'd do something like this and it would work fine, but this is sapui5 odata declaration, and I'm not sure how to deal with it.

junwu
Active Contributor
0 Kudos

don't know what you are talking....

model declaration is just one line code...nothing to wait, after that line,  you can call directly

Former Member
0 Kudos

The model has an event that is triggered when the metadata load was completed. Attach a function to that event and handle everything that has to be done after the the metadata is loaded.

Attaching a function to the event can be done with attachMetadataLoaded(oData?, fnFunction, oListener?)

junwu
Active Contributor
0 Kudos

why bother to wait ?

just CALL IT

dunayevsky_yuri
Participant
0 Kudos

I used this approach.

It's not exactly what I was trying to achieve, but it more or less answers my issue.

Thanks.

junwu
Active Contributor
0 Kudos

why you have to wait until the metadata is loaded?

dunayevsky_yuri
Participant
0 Kudos

I need metadata because in my case to make read() calls I needed the name of the source table which can be obtained from the service's metadata.

I made a function whose only purpose is to do read() calls which receives the required data like the model's name, source table and so on.

junwu
Active Contributor
0 Kudos

what's the point of depending on the metadata? any benefit?

dunayevsky_yuri
Participant
0 Kudos

I have one function which makes read() (read(sPath, mParameters?): object) calls to different OData models and, of course, the path (sPath) to the required data is different every time. I need metadata to get that path dynamically, without hardcoding it.

I didn't find any way to achieve it without using metadata, but if there is one, I'd be glad to learn it.

junwu
Active Contributor
0 Kudos

once you development is done, the spath never change right? it is static right? what's the point to wait for this static info?

SergioG_TX
Active Contributor
0 Kudos

Hi Yuri,

it may also be depending on where you are loading these odata models.

1) are your url and url2 valid URLs?

2) are you doing this on the Component.js file  during the init function? (based on your JS code)

have you tried loading them from the manifest.json file ? 

check out this blog and see if this helps you.

hope it helps!

dunayevsky_yuri
Participant
0 Kudos

Hi,

thank you for the answer.

1) urls are valid. if I change loadMetadataAsync property to false, everything works fine.

2) I define models during onBeforeRendering stage in controller. I plan to use the app with Fiori Launchpad, so I need to do all definitions during onBeforeRendering stage, because onAfterRendering hook runs only once - if I open app on Fiori Launchpad for the second time, onAfterRendering hook won't run.