Skip to Content

Execute Code if Binding has Data

Nov 07, 2017 at 04:37 PM


avatar image

I to an element binding like so

var oModel = oView.getModel();
var oPromiseMetadataLoaded = oModel.metadataLoaded();
oPromiseMetadataLoaded.then(function() {
    var sObjectPath = oModel.createKey("Project", {
        ProjectID: sProjectId
    oView.bindElement("/" + sObjectPath);
    // <HERE>

Now I want to execute a function (marked with '// ' where it should go) which uses data from the bound Object. When the data is not there yet (the model is obviously an OData model), I need to attach to the dataReceived event, but when when the data is already there, this event won't fire.

What is the most (SAPUI5-)idimatic way to execute code in both cases? Is there a Promise like oModel.metadataLoaded()? Do I need to consider something, e.g. to probably not read data from an object previously bound to the view?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Iftah Peretz
Jan 06 at 09:24 PM


Don't know if this is the "most (SAPUI5-)idimatic way" to do it, but it's a way (there are other options)

onInit: function() {
	var oModel = new JSONModel("the path to data"); //Just to get the context
	oModel.attachRequestCompleted(function(oEvent) {
	    var oActiveModel = oEvent.getSource();
	    if (!jQuery.isEmptyObject(oModel.getData())) {
		// <HERE>
10 |10000 characters needed characters left characters exceeded
Ankit Maskara
Jan 07 at 03:30 PM

You need to do processing in the success call back function of the OData Model's read function call.


10 |10000 characters needed characters left characters exceeded