使用REST服务

时间:2018-04-28 17:08:13

标签: sapui5

我正在尝试通过JSON模型加载显示从restdb.io - 数据库中使用的SAPUI5表中的项目。我认为它不起作用,因为一些异步主题,可能数据消耗太晚或类似的东西。如何克服此问题并显示项目?

查看:

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

控制器:

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");
    }
  });
});

console.log中的JSON如下所示:

  

阵列(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}

1 个答案:

答案 0 :(得分:2)

尝试直接在视图上设置模型:

this.getView().setModel(oModel, "items");