Skip to Content

Why is the getContexts() array empty in onInit function of controller?

Jul 26, 2017 at 10:38 AM


avatar image

To present my data in a VizFrame, I have to prepare the data beforehand. The original data is defines in the manifest.json, and a control list view shows the items from the oData properly.

    path : 'origdata>/dataview

In the controller of the view containing the VizFrame, I have something like this:

onInit: function() {
  var oGraphDataModel = new sap.ui.model.json.JSONModel({}, "graphdata");
  var oODataModel = this.getOwnerComponent().getModel("origdata");
  var handler = function(oEvent) {
    var mapCallback = function(oContext) {
      // remmap the original fields orign=>graph
    var reduceCallback = function(aPrev, oCurr) {
      // search and summarize the data
    var binding = oEvent.getSource().bindList("/dataview");
// HERE: binding.getContexts() is empty
    var aTotals = binding.getContexts().map(mapCallback).reduce(reduceCallback, []);
    oGraphDataModel.setData({"Totals" : aTotals	});
  var pViz = this.getView().byId("NastViz");
  var pDataset = new{
    dimensions : [{...}],
    measures : [{...}],
    data : { path: "/Totals" }
  var feedValueAxis = new sap.viz.ui5.controls.common.feeds.FeedItem({
  ... }), 
  feedCategoryAxis = new sap.viz.ui5.controls.common.feeds.FeedItem({
  ... });

The retrieval of the OData with getModel() seems to work, but the call bindList and corresponding getContexts() returns an empty array. The VizFrame shows consequently "No Data".

I checked the binding object, and it contains an array of keys (value akeys), which looks right. getContexts() returns (besides an empty array) the values


Please help me to find the problem with my approach. Thanks in advance.

Edit: I tried to exchange the `attachRequestCompleted` call with `attachMetadataLoaded`. Maybe not yet loaded metadata is the reason for the empty contexts. I added a debug message showing the result of the oODataModel.metadataLoaded() function, which returns


I also check the oODataModel object in the console, and it contains a property called aBindings with 81 entries.

But still, the results for getContexts() is an empty array.

Edit: Since I have a working list with the odata entries, I look into the differences of the models/bindings of the view with the list and the view with the VizFrame. In the list-View the binding is created automatically with the code from the first quote (path: '...'). In onBeforeRendering is observed the odata models in both views and they are equal. But in the list-View I have access to the binding with 'this.getView().byId("listname").getBinding("items")'. How does sapui creates the binding, and how can I do it manually for the view with the VizFrame? I think, I have a basic misunderstanding how binding works in sapui5.

Edit: My current (temporary) solution is the use of the automatically created binding from the list view. In the controller of the view with the list I do

var oB = this.getView().byId("myListName").getBindings("items");
oB.attachDataReceived(function() { 
//my evaluation code -> set data of graphDataModel

The graphDataModel is defined in the Component.js and is made global using the sap.ui.getCore().setModel(graphDataModel) function. Now, the view with the list changes the data, and the view with the VizFrame displays it.

I think, this solution is very dirty, because the selected data from the oData model depends on the items displayed in the list. This causes problems e.g. when using a growing list, where the entries in the binding change with every growing. Does anyone have an idea, how I can get the data from the oDate model without connecting the binding to a displayed part of the view?

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

1 Answer

Felix Lemke Aug 01, 2017 at 07:44 AM

I came to the conclusion that the use of bindings fits not my requirements here. I solved this problem by using

refreshGraphData: funtion() {
  //retrieve models (from getCore())"/dataset", {
    success: function(oEvent) {
    // do all my calculation on the oEvent.results array
    // write results into oGraphData model    

In the onBeforeRendering part of the view/controller containing the VizFrame, I inserted

10 |10000 characters needed characters left characters exceeded