cancel
Showing results for 
Search instead for 
Did you mean: 

Why using metadataLoaded() on a bindElement?

Former Member
0 Kudos

Hi Guru's,

I am doing some trainings in SAPUI5 and also bought the great SAP Press book "SAPUI5 The comprehensive guide". Especially in this book I run into an example where they use a metadataLoaded function on the model when binding data to a view. If I understood it correctly then this function fires when the metadata on the model was loaded so that you know you can access the metadata.

The book uses something like the code example below:

var sSalesOrderID = oEvent.getParameter("arguments").BusinessPartnerID;

oModel.metadataLoaded().then(function() {

       var sObjectPath = this.getView().getModel().createKey("SalesOrderSet", {

            SalesOrderID: sSalesOrderID

       });

       this.getView().bindElement({

            path: "/" + sObjectPath

       })

}.bind(this));


The sObjectPath variable then contains a value like "SalesOrderSet('0500000002')", this works perfectly but I want to understand why I have to use this metadataLoaded method because before I bought the book I followed some openSAP trainings and followed the tutorials on the SAPUI5 website and there I learned to do the same through:

var sSalesOrderID = oEvent.getParameter("arguments").BusinessPartnerID;

this.getView().bindElement({

     path: "/SalesOrderSet('" + sSalesOrderID + "')"

});


Both code samples above work perfectly so the main question I have is: what does the metadataLoaded exactly do and in which situations should you use it (or should you always use it)?

Kind Regards,

Nico van der Linden



Accepted Solutions (1)

Accepted Solutions (1)

JoseMunoz
Active Participant
0 Kudos

Hi,

metadataLoaded was introduced on 1.30 and it ensures that the function you pass is only executed if metadata is loaded successfully.


So if you are below 1.30 you cannot use metadataLoaded.


Below you can do what you say:


var sSalesOrderID = oEvent.getParameter("arguments").BusinessPartnerID;

this.getView().bindElement({

     path: "/SalesOrderSet('" + sSalesOrderID + "')"

});


But then if metadata was not loaded it raises a odata general error.


Regards

Former Member
0 Kudos

Great Jose, thanks for your answer. So if I understand you correctly best practice would be to use the metadataLoaded in all cases from SAPUI5 1.30 and upwards to prevent possible timing issues with loading metadata and using it on the view?

Kind Regards,

Nico van der Linden

JoseMunoz
Active Participant
0 Kudos

Yep, you are right

Regards

Answers (0)