3

I try to get the service metadata of a sapui5 v2 odata model. Code:

var oModel = new sap.ui.model.odata.v2.ODataModel(someServiceURL);
var oMetadata = oModel.getServiceMetadata();

This should work according to this page: https://openui5beta.hana.ondemand.com/docs/guide/6c47b2b39db9404582994070ec3d57a2.html

Anyhow I got "undefined" for oMetadata. If I change code to:

var oModel = new sap.ui.model.odata.v2.ODataModel({
        loadMetadataAsync : false,
        serviceUrl : someServiceURL
});

Still oMetadata === undefined

According to SDK documentation metadata should be loaded in sync:

Return the metadata object. Please note that when using the model with bLoadMetadataAsync = true then this function might return undefined because the metadata has not been loaded yet. In this case attach to the metadataLoaded event to get notified when the metadata is available and then call this function.

What is wrong with my code?

I am using (1.28.11):

<script src="https://sapui5.netweaver.ondemand.com/resources/sap-ui-core.js"    ...

I started debugging the UI5 code and detected following line: this.bLoadMetadataAsync = true;

I started debugging of SAPUI5 code and detected following line (seems to be called each time):

this.bLoadMetadataAsync = true;

Is it a bug? Or is something wrong with my code?

user3783327
  • 616
  • 8
  • 30
  • 60

2 Answers2

4

Solution: The following worked for me in an actual application environment. I guess it not being fired in my fiddle was due to no actual data request being made:

var oModel = new sap.ui.model.odata.v2.ODataModel(<ServiceURL>);

oModel.attachMetadataLoaded(null, function(){
   var oMetadata = oModel.getServiceMetadata();
   console.log(oMetadata);
},null);

Lead up to the solution:

Ok so I started playing around with this a bit and found the following:

  • .getServiceMetadata() worked fine with sap.ui.model.odata.ODataModel.
  • with the sap.ui.model.odata.v2.ODataModel the request for the metadata was sent through the network but somehow .getServiceMetadata() returned undefined.
  • I tried to sap.ui.model.odata.v2.ODataModel.attachMetadataLoaded() but the event was never fired. (This only applied in the jsbin I used)

I will edit this with any further findings I make. If you have anything that should be included in my findings/testing just tell me.

Edit:

The bLoadMetadataAsync is a parameter you can set on the sap.ui.model.odata.ODataModel. The parameter is not in the API for sap.ui.model.odata.v2.ODataModel anymore. I assume that the async loading has been choosen as default.

Edit: @user3783327 Reported a bug here: https://github.com/SAP/openui5/issues/564

Carsten
  • 1,511
  • 2
  • 13
  • 24
  • Thanks, was a typo in the issue. Of course I used the same variable. I fixed the text. – user3783327 Aug 11 '15 at 12:40
  • Did some diging into this. Edited my answer. – Carsten Aug 11 '15 at 13:48
  • Than the documentation is wrong or we detected a bug? Compare that page: https://openui5beta.hana.ondemand.com/docs/guide/6c47b2b39db9404582994070ec3d57a2.html – user3783327 Aug 11 '15 at 13:58
  • There is no mention of the not async loading being available on the 'v2.OdataModel'. – Carsten Aug 11 '15 at 14:15
  • 1
    https://sapui5.netweaver.ondemand.com/sdk/#docs/api/symbols/sap.ui.model.odata.v2.ODataModel.html#getServiceMetadata says _Return the metadata object. Please note that when using the model with bLoadMetadataAsync = true then this function might return undefined because the metadata has not been loaded yet. In this case attach to the metadataLoaded event to get notified when the metadata is available and then call this function. _ – user3783327 Aug 11 '15 at 14:47
  • Yes while this looks a lot like copy pasta it is not necessary false. It just fails to mention that (as you discovered) the `bLoadMetadataAsync` is always `true` for the `v2.ODataModel`. Lets see what the raised issue yields as a result. – Carsten Aug 11 '15 at 14:54
  • 1
    The next version of UI5 will have a method on the model that returns a promise: https://openui5beta.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.v2.ODataModel.html#metadataLoaded This makes it easier to use the completely asynchronous v2 model: oModel.metadataLoaded().then(function() { [...] }); – sirion Aug 16 '15 at 18:10
0

As sirion already mentioned, the ODataModel has now an API named metadataLoaded which returns a promise accordingly. In the resolve function, we can definitely get the service metadata via getServiceMetadata().

myODataModel.metadataLoaded()
  .then(() =>/* Do something with */myODataModel.getServiceMetadata());

Alternatively, we can also make use of ODataMetaModel which can be set on any ManagedObject (including View) and provides several useful accessors related to the service metadata. In order to get the meta model, we need to use the appropriate API from the ODataModel instead of instantiating the model directly:

myODataModel.getMetaModel().loaded()
  .then(() =>/* Do something with */myODataModel.getMetaModel()/*...*/);

Documentation: Meta Model for OData V2

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170