How to Check If an OData Model Is Loaded in SAP UI5?

UI5

Two best practice options exist to check if an OData model is loaded in SAP UI5 and therefore, holds the data:

  1. success and errors 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 SAP UI5 OData model request.

For example, a read request:

// in your controller

...
 
onInit: function() {
   
  var oModel = this.getOwnerComponent().getModel("modelName");
   
  oModel.read("/path", {
         
    success: function(oData, oResponse) { 
       
      // request completed
      // data loaded             
      // oData.results contains the read data
      // oResponse contains further request response informations
 
    },

    error: function(oError) {

      // request completed
      // data not loaded
      // oError contains request error informations

    }

  }); 

}

...

Request Completed Event (Batch Request Completed Event)

Attach the request completed event to an SAP UI5 OData model to see if a single request is completed. The event works for both manual requests and requests which are automatically fired by the SAP UI5 framework due to binding to an SAP UI5 control.

If the request is not a batch request, then the request completed event is to attach to the model. If the request is a batch request, then the batch request completed event is to attach 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 batch request completed event.

The request completed and batch request completed 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 request completed 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
 
  }); 
 
...
No Comments

Most Recent Articles

Will SAP UI5 replace the SAP Web UI?

SAP Full Forms

More Similar Articles

Leave a Reply

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.

Menu