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

Recommendations for how to call oModel.read due to the async nature of it.

0 Kudos
4,592

Hello community experts,

I am invoking an odata read call which fetches me a set of data. In order to complete my data set I need to make subsequent read calls to another odata service passing in input from the first read call. However, I (think I) need to call the second read call in a loop to fetch the relevant data. Here is an example of what I'm doing:

oModel.read("/myService", {
   success: function(oData) {
      var dataArray = oData.array;
      for (var i = 0; i < dataArray.length; i++) {
         var dataRow = dataArray[i];
         for (var j = 0; j < dataRow.length; j++) {
            oModel.read("/nextService('" + dataRow[j].input + "')", {
               success: function(oData) {
                  dataRow[j].element = oData.sVar;
               },
               error: function(oError) {
               }
            });
            dataArray[i].dataRow = dataRow; // might not be necessary, but I have tried without it too.
         }
      }
   },
   error: function(oError) {
   }
});

I'm most likely guessing that what I'm attempting is bad practice, not recommended etc etc. I get all that, I don't know or understand enough about the lifecycle of the app and the async nature of the ODataModel to know how to tackle this and so need the communities input. Bottom line is, I need for a way to get data from one read call (works) and loop through and pass input to make subsequent read calls to a second service from which the data retrieved will be assigned back to an array from the first call (which is missing). During debugging I found that both 'for loops' complete running before the second read call even completes once (due to async). I have also tried implementing the function 'attachRequestCompleted' but that didn't solve it for me. What am I doing wrong and what is the best practice to achieve what I need?

Thanks

D.

View Entire Topic
ThorstenHoefer
Active Contributor

Hi,

please consider navigation properties and use a oData Call with the expand parameter:

oModel1.read("/ProductSet", {
    urlParameters: {
        "$expand": "ToSupplier"
    },
    success: function(data, response) {
    },
    error: function(oError) {
    }
});

or

<Table items="{
  path: '/ProductSet',
  parameters: {
    expand: 'ToSupplier'
  }
}">



0 Kudos

Thorsten,

The service endpoints are different entities so cannot use $expand. I think you might be suggesting to change the odata implementation itself? If so, we cannot as its not maintained by us.