Search code examples
javascriptodatasapui5

Reading OData contexts in onInit of controller


I've tried to prepare data from an OData source to show it in a bar graph in my fiori app. For this, I setup the OData model in the manifest.json. A test with a list, simply using

items="{path : 'modelname>/dataset'}

works fine and shows the content.

To prepare data for a diagram (VizFrame), I used the onInit() function in the controller of the view (mvc:XMLView). The data preparation is similar to the one discussed in question.

At first I obtain the ODataModel:

var oODataModel = this.getOwnerComponent().getModel("modelname");

Next I do the binding:

var oBindings = oODataModel.bindList("/dataset");

Unfortunately, the oBindings().getContexts() array is always empty, and also oBindings.getLength() is zero. As a consequence, the VizFrame shows only "No Data".

May it be that the data model is not fully loaded during the onInit() function, or do I misunderstand the way to access data?

Thanks in advance

Update I temporary solved the problem by using the automatically created bind from the view displaying the data as list. I grep the "dataReceived" event from the binding getView().byId("myList").getBindings("items") and do my calculation there. The model for the diagram (since it is used in a different view) is created in the Component.js, and registered in the Core sap.ui.getCore().setModel("graphModel"). I think this solution is dirty, because the graph data depends on the list data from a different view, which causes problems, e.g. when you use a growing list (because the data in the binding gets updated and a different range is selected from the odata model). Any suggestions, how I can get the odata model entries without depending on a different list?


Solution

  • May it be that the data model is not fully loaded during the onInit() function, or do I misunderstand the way to access data?

    You could test if your model is fully loaded by console log it before you do the list binding

    console.log(oODataModel);
    var oBindings = oODataModel.bindList("/dataset");
    

    If your model contains no data, then that's the problem.