Skip to Content

Execute Code if Binding has Data

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jan 06 at 09:24 PM

    Hi,

    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
    	this.getView().setModel(oModel);
    	oModel.attachRequestCompleted(function(oEvent) {
    	    var oActiveModel = oEvent.getSource();
    	    if (!jQuery.isEmptyObject(oModel.getData())) {
    		// <HERE>
    		}
    	     });
           },
         
    Add comment
    10|10000 characters needed characters exceeded

  • 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.

    syntax

    Add comment
    10|10000 characters needed characters exceeded