UI5 – Reading oData Models into JSON

Home / Blog / UI5 – Reading oData Models into JSON

My UI5 journey has gone slower than I would like, but I’m still making steady progress.  My latest obstacle to overcome was being able to read in my configuration tables, so that I could use them to dynamically alter what is shown on the screen.  Obviously, the first thing I wanted to do was to be able to read in those tables, but I had a secondary objective, and that was to locally store all the values so that I wouldn’t need to read them in every time the application fired off.  It took some digging, but I finally got there.

// Config Settings
// Using a local model for offline data capture
var oConfig = new sap.ui.model.json.JSONModel();
jQuery.sap.require(“jquery.sap.storage”);
//Check if there is data in the Storage
if (oStorage.get(“config”)) {
var oDataConfig = oStorage.get(“config”);
oConfig.setData(oDataConfig);
} else {
var urlCon = “Config”;
oModel.read(urlCon, null, null, false, function(oData, oResponse) {
oConfig.setData(oData);
}, null);
}
var oDataConfig = oConfig.getData();
oConfig.refresh(true);
oStorage.put(“config”, oDataConfig);
oView.setModel(oConfig, “config”);

 

so, if you take a look at this, the first section creates a JSON model (please note, that I have already connected to the service that contains everything.  Now I just need to read it.  the jquery.sap.storage is the cool class that is provided to locally store things in a browser.  You will have nothing in here the first time the app is run from any browser (or if you clear the cache), but once it’s there, it will be read in, without a service call.  The if statement above is what handles that.  Note, that the url will have everything from the original model, I just needed to tell it what service to execute.  In my case, Config, with no inputs.  After the If statement, it is putting the data into the JSON model, and making the model available to the rest of the application.

Pretty easy, once you know the tricks.  Next up, dynamically creating input and output fields using this data.  But I have more homework to do before I can talk about that.

Thanks for reading,

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

Leave a Reply

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