如何在控制器中从SAPUI5 V1.38.39中的本地JSON文件创建JSON模型?

问题描述 投票:0回答:1

环境:

框架:SAPUI5 V.1.38.39IDE:WebIde

要求:

我在模型文件夹中创建了一个JSON文件Data.json,我想在我的控制器onInit中使用创建JSON模型] >,我的实际问题是在Data.json上总是向我返回404响应文件夹树它是初始的来自SAPUI5应用程序模板

代码和测试问题:

test 1在webapp / controller / App.contoller.js中:

onInit: function () {
  var oBusy = new sap.m.BusyDialog();
  var oModel = new JSONModel();
  oModel.attachRequestSent(function() {
    oBusy.open();
  });
  oModel.loadData("model/Data.json"); //here the path so I suppose here the problem
  oModel.attachRequestCompleted(function() {
    oBusy.close();
  });
  this.getView().setModel(oModel, "CompaniesModel")
}

test 2

在webapp / controller / App.contoller.js中:
onInit: function () {
  var oBusy = new sap.m.BusyDialog();
  var oModel = new JSONModel();
  oModel.attachRequestSent(function() {
    oBusy.open();
  });
  oModel.loadData("../model/Data.json"); //here the path so I suppose here the problem
  oModel.attachRequestCompleted(function() {
    oBusy.close();
  });
  this.getView().setModel(oModel, "CompaniesModel")
}

test 3

在webapp / controller / App.contoller.js中:
onInit: function () {
  var oBusy = new sap.m.BusyDialog();
  var oModel = new JSONModel();
  oModel.attachRequestSent(function() {
    oBusy.open();
  });
  oModel.loadData("my/namespace/model/Data.json"); //here the path so I suppose here the problem
  oModel.attachRequestCompleted(function() {
    oBusy.close();
  });
  this.getView().setModel(oModel, "CompaniesModel")
}

附录-复制过程:

在用于SAPUI5的Webide中,从模板SAPUI5应用程序选择SAP版本1.38.39中创建新的,初始化您的名称空间和视图/控制器名称。在模型中创建新的Data.json:

{
    "Set1": [{
        "Status": "status1",
        "Number": 10
    }, {
        "Status": "status2",
        "Number": 20
    }, {
        "Status": "status3",
        "Number": 30
    }, {
        "Status": "status4",
        "Number": 40
    }]
}

您认为:

<mvc:View 
    controllerName="your.namespace.controller.App" 
    xmlns:l="sap.ui.layout" 
    xmlns:core="sap.ui.core" 
    xmlns:mvc="sap.ui.core.mvc"
    xmlns:viz="sap.viz.ui5.controls"
    xmlns:viz.feeds="sap.viz.ui5.controls.common.feeds"
    xmlns:viz.data="sap.viz.ui5.data"
    xmlns:com="sap.suite.ui.commons" 
    xmlns="sap.m">
    <Shell id="shell">
        <App id="app">
            <pages>
                <Page id="page" title="{i18n>title}">
                    <content>
                        <l:VerticalLayout class="sapUiContentPadding" width="100%">
                            <com:ChartContainer id="chartContainer" showFullScreen="true" showPersonalization="false" autoAdjustHeight="false"
                                personalizationPress="attachPersonalizationPress" contentChange="attachContentChange" title="chart">
                                <com:content>
                                    <com:ChartContainerContent icon="sap-icon://line-chart" title="Line Chart">
                                        <com:content>
                                            <viz:VizFrame id="idVizFrame_1" height='400px' width="100%" vizType='column' uiConfig="{applicationSet:'fiori'}">
                                                <viz:dataset>
                                                    <viz.data:FlattenedDataset data="{CompaniesModel>/Set1}">
                                                        <viz.data:dimensions>
                                                            <viz.data:DimensionDefinition name="Status" value="{CompaniesModel>Status}"/>
                                                        </viz.data:dimensions>
                                                        <viz.data:measures>
                                                            <viz.data:MeasureDefinition name="Number" value="{CompaniesModel>Orders}"/>
                                                        </viz.data:measures>
                                                    </viz.data:FlattenedDataset>
                                                </viz:dataset>
                                                <viz:feeds>
                                                    <viz.feeds:FeedItem uid="valueAxis" type="Measure" values="Number"/>
                                                    <viz.feeds:FeedItem uid="categoryAxis" type="Dimension" values="Status"/>
                                                </viz:feeds>
                                            </viz:VizFrame>
                                        </com:content>
                                    </com:ChartContainerContent>
                                </com:content>
                            </com:ChartContainer>
                        </l:VerticalLayout>
                    </content>
                </Page>
            </pages>
        </App>
    </Shell>
</mvc:View>

在您的控制器中:

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

    return Controller.extend("your.namespace.controller.App", {
        onInit: function () {
          //copy here one of the controller described in Problem in code & test
        }
    });
});

编辑(清单以外的其他选项):

如果您不想使用清单,我在这里添加了这种可能性,要使用清单,请转至@Cmdd的可接受答案:)如果您不想使用清单,可以执行:

oModel.loadData("../../../../../webapp/model/Data.json");

环境:框架:SAPUI5 V.1.38.39 IDE:WebIde要求:我在模型文件夹中创建了一个JSON文件Data.json,我想在控制器的onInit中使用它来创建一个...

javascript json sapui5
1个回答
1
投票

最佳方法是将数据加载到manifest.json中,如this链接中所述。

© www.soinside.com 2019 - 2024. All rights reserved.