Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAP UI5 access value of oData model

I'm getting started with SAPUI5 these weeks and built my first app that displays some data. I even managed to do some filtering but now I encounter an error that I do not understand.

My oData Model has the following structure:

Notice('0000'):
  Value0: abc
  Value1: def
  Value2: ghi


Notice('0001'):
  Value0: abc
  Value1: def
  Value2: ghi

I have a table holding the data:

<Table
    id="noticeList"
    class="sapUiResponsiveMargin"
    width="auto"
    items="{
        path : '/Notice',
        sorter : {
            path : 'Value0'
        }
    }">

...some header stuff and column delaration...

      <items>
        <ColumnListItem
             id="noticeColumnListItem"
             items="{
                path : '/Notice'
            }">
            <cells>
                <ObjectStatus text="{Value0}"/>
                <ObjectStatus text="{Value1}"/>
                <ObjectStatus text="{Value2}"/>
           </cells>
        </ColumnListItem>
    </items>
</Table>

Now, I want to access the Data of my Model (no matter if before or after rendering). I do not have a event to trigger this, I just need the information as soon as the model data was transferred.

So I searched a little and found the following:

var myValue = this.byId("noticeList").getModel().getProperty("/Notice/Value0");

I tried every possible path but always end up in that myValue is undefined. When I dump my model, I can see that it holds an object oData with the needed values. Also, my list is dispayed correct but I just don't manage to read a single value from the Model.

This is how I instanciate it in Component.js:

config: {
    fullWidth: true,
    resourceBundle: "i18n/messageBundle.properties",
    serviceConfig: {
        name: "MYJOBS",
        serviceUrl: "/sap/opu/odata/sap/PATH_TO_SERVICE"
    }
},

// Read service URL
var sServiceUrl = mConfig.serviceConfig.serviceUrl;

// Create and set domain model to the component

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, {
    json: true,
    loadMetadataAsync: true
});

oModel.attachMetadataFailed(function() {
    sap.ui.getCore().getEventBus().publish("Component", "MetadataFailed");
}, this);

//var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
this.setModel(oModel);

Maybe I should also mention that I don't use index.html to load the app but do this via Fiori Launchpad and Component.js

I spent hours over hours on this problem and can't find a solution, I hope someone can help...

Kind regards!

like image 422
Tobias Kleffmann Avatar asked Oct 21 '25 12:10

Tobias Kleffmann


1 Answers

If you are calling a remote service the most common cause of an undefined scenario is the delay in returning the data with the other code continuing to execute (as you are calling your service asynchronously).

If you are trying to act on the returned data then you could do this by attaching an event listener / anonyomous function in the following manner:

oModel.attachRequestCompleted(function(){
                    // do something
                });

In your scenario your controller code could be changed to look as follows:

 onInit: function() {

   var sServiceUrl = "/sap/opu/odata/sap/yourPathSRV/";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
   oModel.attachRequestCompleted(function(){
                    // do something
                });
   this.getView().setModel(oModel);
}
like image 170
Bernard Avatar answered Oct 23 '25 07:10

Bernard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!