0

I am trying to display items in an SAPUI5 table consumed from a restdb.io - database via JSON model load. I assume that it does not work because of some async topics, maybe data is consumed too late or something like this. How can I overcome this issue and display the items?

View:

<mvc:View
  controllerName="IR_Voting_V1.controller.Main"
  xmlns:mvc="sap.ui.core.mvc"
  xmlns="sap.m"
  displayBlock="true">
  <App>
    <Page title="{i18n>title}">
      <List id="contactList"
        width="auto"
        items="{items>/}">
        <ObjectListItem title="{items>title}" />
      </List>
    </Page>
  </App>
</mvc:View>

Controller:

sap.ui.define([
  "sap/ui/core/mvc/Controller"
], function(Controller) {
  "use strict";

  return Controller.extend("IR_Voting_V1.controller.Main", {
    onInit: function() {
      var sUri = "https://myurl-xxxx.restdb.io/rest/mydb";
      var oHeaders = {
        "Content-Type": "application/json",
        "x-apikey": "myCORSkey",
        "Cache-Control": "no-cache"
      };
      var oModel = new sap.ui.model.json.JSONModel();
      oModel.loadData(sUri, null, true, "GET", null, false, oHeaders);
      console.log("model", oModel);
      sap.ui.getCore().setModel(oModel, "items");
    }
  });
});

JSON in console.log looks like this:

Array(7)

0: {_id: "5ae31847b8552b5200008ef0", title: "AA", ident: "IR07", votings: 0}
1: {_id: "5ae317e5b8552b5200008ee7", title: "BB", ident: "IR03", votings: 0}
2: {_id: "5ae317fab8552b5200008ee8", title: "CC", ident: "IR04", votings: 0}
3: {_id: "5ae31809b8552b5200008eea", title: "DD", ident: "IR05", votings: 0}
4: {_id: "5ae3181eb8552b5200008eeb", title: "EE", ident: "IR06", votings: 0}
5: {_id: "5ae317a4b8552b5200008ee2", title: "FF", ident: "IR02", votings: 0}
6: {_id: "5ae31772b8552b5200008edf", title: "GG", ident: "IR01", votings: 0}
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
dotchuZ
  • 2,621
  • 11
  • 39
  • 65
  • If you're working with Components, try to **avoid setting models on the Core**. More about model propagation..: https://stackoverflow.com/a/42251431/5846045 – Boghyon Hoffmann Apr 28 '18 at 18:29
  • 1
    Possible duplicate of [Accessing Global Model from Controller's onInit Method](https://stackoverflow.com/questions/33121909/accessing-global-model-from-controllers-oninit-method) – Boghyon Hoffmann Apr 28 '18 at 18:31
  • Two questions are [considered as duplicates](https://meta.stackexchange.com/a/10844) if they both express the same problem and are solvable by the same (or equivalent) solution. In this case, the view wasn't aware of the model `"items"`. The reason behind this as well as possible solutions are mentioned in the answer that I gave to a question that described the same issue. Hence, duplicate. It's not bad, though, if your question get's marked as such. You just expressed it differently which surely helps future readers who might be experiencing a similar problem. :) Duplicates won't be deleted. – Boghyon Hoffmann Apr 28 '18 at 23:25

1 Answers1

2

Try to set model directly on a view:

this.getView().setModel(oModel, "items");
Andrew Naumovich
  • 1,441
  • 1
  • 12
  • 15