Building a mobile app using OpenUI5 and Cordova using framework like jQuery.sap.storage

You are thinking correctly for your requirement. What you can do is, use the read method for each entityset to read the oData. In the success callback for this method, you can parse the result objects into a corresponding JSON model.

Now, you can have working logic as per which ?

  • When you are online, use the additional layer to fill the JSON model from the oData that has been read
  • When offline, you can read from local storage

Implementation Approach

The jQuery.sap.storage API provides local storage capabilities for your mobile app. Here's how to implement the online/offline functionality ?

Example

Below is a complete implementation showing how to handle both online and offline scenarios ?

// Initialize storage
var oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);

// Function to handle online data reading
function readOnlineData() {
    var oModel = this.getView().getModel();
    
    // Read from OData service
    oModel.read("/ProductSet", {
        success: function(oData) {
            // Parse and store data locally
            var jsonData = JSON.stringify(oData.results);
            oStorage.put("ProductData", jsonData);
            
            // Create JSON model
            var oJSONModel = new sap.ui.model.json.JSONModel(oData.results);
            this.getView().setModel(oJSONModel, "products");
        }.bind(this),
        error: function(oError) {
            console.error("Failed to read online data");
            this.readOfflineData();
        }.bind(this)
    });
}

// Function to handle offline data reading
function readOfflineData() {
    var storedData = oStorage.get("ProductData");
    
    if (storedData) {
        var parsedData = JSON.parse(storedData);
        var oJSONModel = new sap.ui.model.json.JSONModel(parsedData);
        this.getView().setModel(oJSONModel, "products");
    }
}

// Check connectivity and load data accordingly
if (navigator.onLine) {
    readOnlineData();
} else {
    readOfflineData();
}

The output shows your mobile app seamlessly switching between online and offline modes ?

Online: Data fetched from OData service and stored locally
Offline: Data retrieved from local storage
Model successfully bound to view in both scenarios

Conclusion

This approach ensures your OpenUI5 Cordova mobile app works efficiently in both online and offline modes, providing a seamless user experience by leveraging jQuery.sap.storage for local data persistence.

Updated on: 2026-03-13T18:27:14+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements