Search code examples
javascriptodatasapui5

Using read function of oData model in UI5


I am coding an UI5 App which consumes a given OData Service. Now I want to get the name of an account with a given account number and Display it in a table. As I can only access the account Name via /AccountInfoSet()/ShortText I tried to use a formatter function to map the account number.

Binding in View:

Formatter function in Controller:

numToNameFormatter : function(sNum){ var text = this.getView().getModel().read("/AccountInfoSet('" + sNum + "')", null, null, true, function(oData, oResponse){ return JSON.stringify(oData); }, function(){ alert("Read failed"); }); return text; }

This should return the requested object as a string. The data is requested successfully, as I verified via an alert. The problem is, that I can't get the data out of the call back, as it ist asynchronous. How do I get the data.

Thanks in advance!


Solution

  • Not sure if your data model is set up like this, but would it be possible to expand your table set to also load the related AccountInfoSet's?

    I mean, if your table holds for instance an array of Accounts, and each Account entry has a related AccountInfo, you could just fill your table with the following:

    http://your.service/Accounts/?$expand=AccountInfo

    You then bind the field in your table directly, without a formatter:

    <TextField value="{AccountInfo/0/ShortText}">