admin管理员组

文章数量:1405584

I am trying to achieve the asynchronous function calls for reading the odata in SAPUI5.

here is a example:

    this.odataModel = new sap.ui.model.odata.ODataModel(oDataUrl, true, 'user', 'pwd');
   /* data retrieving time 5sec */
    function read1(){
      this.odataModel.read('entity-url1',null, null, true, function(data){
       console.log('success');
        return data;
      },
      function(){ console.log('error'); });
    }
       /* data retrieving time 10sec */
    function read2(){
      this.odataModel.read('entity-url2',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }

   /* data retrieving time 10sec */
    function read3(){
      this.odataModel.read('entity-ur3',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }
/* function calls */
 var d1 = read1(); 
 var d2 = read2();
 var d3 = read3();

after this call i am trying to get the data from read3 function i am getting empty data. becuse of execution time.

How can i wait for until execution is pleted.

I am trying to achieve the asynchronous function calls for reading the odata in SAPUI5.

here is a example:

    this.odataModel = new sap.ui.model.odata.ODataModel(oDataUrl, true, 'user', 'pwd');
   /* data retrieving time 5sec */
    function read1(){
      this.odataModel.read('entity-url1',null, null, true, function(data){
       console.log('success');
        return data;
      },
      function(){ console.log('error'); });
    }
       /* data retrieving time 10sec */
    function read2(){
      this.odataModel.read('entity-url2',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }

   /* data retrieving time 10sec */
    function read3(){
      this.odataModel.read('entity-ur3',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }
/* function calls */
 var d1 = read1(); 
 var d2 = read2();
 var d3 = read3();

after this call i am trying to get the data from read3 function i am getting empty data. becuse of execution time.

How can i wait for until execution is pleted.

Share Improve this question asked Jul 1, 2015 at 11:10 manimani 1,0447 gold badges27 silver badges49 bronze badges 3
  • SAPUI5 is a model driven framework. So why not store your data in a model instead of variables? Once everything is loaded and stored in a model, you can access it more easily – Qualiture Commented Jul 1, 2015 at 12:14
  • yeah i did. In my case i am using these three functions in different pages. When i try to reload the page if the data is available it is binding if not it is showing the empty values. how can i fix this issue. – mani Commented Jul 1, 2015 at 12:20
  • What do you mean, 'reload'? As in refresh the browser? That is not going to work... you should handle your data in either the onAfterRendering event handler(s) of your view(s), or in the data retrieval callback methods. Or, if you bind the views controls to your model, the data will be displayed instantly without any extra coding needed – Qualiture Commented Jul 1, 2015 at 12:55
Add a ment  | 

3 Answers 3

Reset to default 2

You won't see any data because your requests are performed asynchronously! If you want to process the data you have to do this in the success callback of the model's read method. But even with a synchronous call you won't see data, as your methods read1, read2, read3 do not return the data.

You're seeing this because the sap.ui.model.odata.ODataModel.read() calls are asynchronous by default - while the read in your read1() call is being executed, the Javascript event queue is continuing processing and executing the remainder of your code. This asynchronous execution is a Good Thing, but you need to cater for it in your code.

There are a few solutions, but the easiest is to only initiate the second read once the first has executed and only initiate the third once the second has pleted. You can do this by modifying your success handlers.

So, a very crude adjustment to read1():

function read1(){
  this.odataModel.read('entity-url1',null, null, true, function(data){
    console.log('success');
    read2();
    return data;
  },
  function(){ console.log('error'); });
}

Then, do the same for read2() and modify your caller to only call read1(). Of course, you'll need to adjust how you return the data to the caller.

A bit untidy, but I hope the above shows the gist of what I'm trying to describe.

You can use jQuery or native promise

//jQuery version
function read1() {
  var promise = jQuery.Deferred();
  this.odataModel.read('entity-url1', null, null, true, function(data) {
      console.log('success');
      promise.resolve(data);
    },
    function() {
      console.log('error');
    });

  return promise;
}

var d1 = read1(); // d1 promise;

d1.then(function(data) {
  console.log(data);
});

本文标签: javascriptTrying to achieve asynchronous read calls on oData in SAPUI5Stack Overflow