简体   繁体   English

尝试在SAPUI5中的oData上实现异步读取调用

[英]Trying to achieve asynchronous read calls on oData in SAPUI5

I am trying to achieve the asynchronous function calls for reading the odata in SAPUI5. 我正在尝试实现异步功能调用,以读取SAPUI5中的odata。

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. 在此调用之后,我尝试从read3函数获取数据,而获取空数据。 becuse of execution time. 因为执行时间。

How can i wait for until execution is completed. 我如何等待执行完成。

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. 如果要处理数据,则必须在模型的read方法的成功回调中执行此操作。 But even with a synchronous call you won't see data, as your methods read1, read2, read3 do not return the data. 但是即使进行同步调用,您也不会看到数据,因为方法read1,read2,read3不会返回数据。

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. 您之所以会这样,是因为sap.ui.model.odata.ODataModel.read()调用在默认情况下是异步的-在执行read1()调用中的读取操作时,Javascript事件队列正在继续处理并执行其余操作您的代码。 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 completed. 有一些解决方案,但最简单的方法是仅在第一个读取执行后才启动第二个读取,而在第二个读取完成后才启动第三个读取。 You can do this by modifying your success handlers. 您可以通过修改成功处理程序来做到这一点。

So, a very crude adjustment to read1() : 因此,对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() . 然后,对read2()执行相同的操作, read2()您的调用者修改为仅调用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或本机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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM