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

🏷️

This is about how to know when an OData model request is 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 the next time, then you are in the right place.

So without further ado, let’s do this!

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

Two best practice options exist to check if an OData request is finished in SAPUI5 and therefore, holds the data:

  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, then 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 is completed. The event works for both manual requests and requests which are automatically fired by the SAPUI5 framework due to binding to an SAPUI5 control.

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

A batch request happens when multiple requests get bundled into one request. A not batch request is just a single request on its own.

OData V2 and V4 automatically make batch requests. Therefore, if you do not manually fire a single OData request then usually it is a batch request and you need to use the batchRequestCompleted event.

The requestCompleted and batchRequestCompleted events fire no matter whether the request succeeded or failed—it just counts if the request completed.

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

For example, 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 example, 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 all.

🙌 More Articles

🔥 The Complete SAP ERP Modules Overview Infographic in PDF Format (Free)

Plus, don't miss future infographics and free SAP tips and insights that are available only to newsletter subscribers. 

Leave a Comment

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu