SAPUI5: How to Know When an OData Model Request Is Loaded?

This is about how to know when an OData model request was loaded in SAPUI5.

You’ll learn:

  • The two best practices to get notified when an OData model request is loaded
  • How to implement those two best practices

So if you want to know when your OData model request is loaded, then you are in the right place.

Let’s jump right in!

Two Best Practices to Get Notified When an OData Model Is Loaded in SAPUI5

Two best practice exist to see if an OData request is finished in SAPUI5:

  1. Success and error callbacks of the request
  2. Request completed event (batch request completed event)

Success and Error Callbacks of the Request

If you do a manual CRUD (Create Read Update Delete) request, you can use the success and error callbacks of the SAPUI5 OData model request.

For example, a read request:

// in your controller
 
...
  
onInit: function() {
    
  var oModel = this.getOwnerComponent().getModel();
    
  oModel.read("/path", {
          
    success: function(oData, oResponse) { 
        
      // request completed
      // data loaded             
      // oData.results contains the read data
      // oResponse contains request response information
  
    },
 
    error: function(oError) {
 
      // request completed
      // data not loaded
      // oError contains request error information
 
    }
 
  }); 
 
}
 
...

Batch Request Completed Event or Request Completed Event

Attach the batchRequestCompleted event to an SAPUI5 OData model to see if a request has been completed. The event works both for manual requests and for those automatically sent by the SAPUI5 framework due to the binding of an SAPUI5 control.

If the request is not a batch request, you need to attach the requestCompleted event to the model.

Batch requests happen when multiple requests are bundled into one request. Non-batch requests are requests that are handled individually.

Batch requests are the default in OData V2 and V4.Therefore, when you don’t manually fire an OData request, it’s usually a batch request and you have to use the batchRequestCompleted event.

The requestCompleted and batchRequestCompleted events fire regardless of whether the request succeeds or fails—it just counts if the request completes.

You can check whether the request succeeded or failed in the oEvent parameter of the callback function.

For instance, the requestCompleted event for a single request:

// in your controller 
 
...
 
onInit: function() {     
    
  var oModel = this.getOwnerComponent().getModel();          
    
  oModel.attachRequestCompleted(function(oEvent) {         
    
    // request completed
    // check oEvent.getParameters("success") ...
    // ... whether the request was successful and the data was loaded
  
  }); 
  
...

For instance, the batch request completed event for a bundle of requests:

// in your controller 
 
...
 
onInit: function() {     
    
  var oModel = this.getOwnerComponent().getModel();          
    
  oModel.attachBatchRequestCompleted(function(oEvent) {         
    
    // request completed
    // check oEvent.getParameters("success") ...
    // ... whether the request was successful and the data was loaded
  
  }); 
  
...

That’s it.

Leave a Comment