远程和本地OData服务

时间:2016-12-05 19:05:19

标签: sapui5

如何配置manifest.json文件,这样当我运行mockserver(mockserver.html)时,它会转到本地json数据,当我运行index.html(主应用程序的主条目)时,它会转到远程服务。我从文档中有一个示例manifest.json文件,但不太清楚远程和本地服务是如何发挥作用的。

{
"_version": "1.1.0",
"sap.app": {
    "_version": "1.1.0",
    "id": "xxx",
    "type": "application",
    "title": "{{appTitle}}",
    "description": "{{appDescription}}",
    "applicationVersion": {
        "version": "1.0.0"
    },
    "dataSources": {
                "mainService": {
                    "uri": "https://services.odata.org/V2/Northwind/Northwind.svc/",
                    "type": "OData",
                    "settings": {
                        "odataVersion": "2.0",
                        "localUri": "localService/metadata.xml"
                    }
                }
            }
},

"sap.ui": {
    "_version": "1.1.0",
    "technology": "UI5",
    "icons": {
        "icon": "",
        "favIcon": "",
        "phone": "",
        "phone@2": "",
        "tablet": "",
        "tablet@2": ""
    },
    "deviceTypes": {
        "desktop": true,
        "tablet": true,
        "phone": true
    },
    "supportedThemes": [
        "sap_hcb",
        "sap_bluecrystal"
    ]
},

"sap.ui5": {
    "_version": "1.1.0",
    "rootView": {
        "viewName": "xxx.view.Main",
        "type": "XML"
    },
    "dependencies": {
        "minUI5Version": "1.30.0",
        "libs": {
            "sap.ui.core": {},
            "sap.m": {},
            "sap.ui.layout": {}
        }
    },
    "contentDensities": {
        "compact": true,
        "cozy": true
    },
    "config": {
          "productLocal": "localService/mockdata/products.json",
          "productRemote": "/some remote end point"
        },

    "products": {
            "dataSource": "mainService"
        }
}

}

控制器代码

var oModel = new sap.ui.model.odata.ODataModel("/", true);

                //var data = oModel;
                //console.log(data);
                var inputModel = new JSONModel("../model/inputs.json");
                var productsModel = new JSONModel();

                oModel.read("/ProductSet",
                    null,
                    null,
                    false,
                    function _OnSuccess(oData, response) {
                        console.log(oData);
                        console.log(response);
                        var data = {"ProductCollection" : oData.results};
                        productsModel.setData(data);


                    },
                    function _OnError(error) {
                        console.log(error);
                    });

                //set model(s) to current xml view
                this.getView().setModel(inputModel, "inputModel");
                this.getView().setModel(productsModel);

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您无需触摸manifest.json文件即可模拟该服务。 实际上,在manifest.json中,dataSources- localUri的属性:将相对URL带到本地元数据文档或注释uri。不是为了服务。

我希望你的mockserver.html看起来像这样:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta charset="utf-8">
        <title>MockServerTutorial</title>
        <script id="sap-ui-bootstrap"
            src="../../resources/sap-ui-core.js"
            data-sap-ui-libs="sap.m"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-xx-bindingSyntax="complex"
            data-sap-ui-resourceroots='{"sap.ui.demo.MockServer": "../"}'>
        </script>
        <link rel="stylesheet" type="text/css" href="../css/style.css">
        <script>
            sap.ui.getCore().attachInit(function() {
                sap.ui.require([
                    "sap/ui/demo/MockServer/localService/mockserver",
                    "sap/m/Shell",
                    "sap/ui/core/ComponentContainer"
                ], function (mockserver, Shell, ComponentContainer) {
                    mockserver.init();
                    new Shell({
                        app: new sap.ui.core.ComponentContainer({
                            height : "100%",
                            name : "MockServerTutorial"
                        })
                    }).placeAt("content");
                });
            });
        </script>
    </head>
    <body class="sapUiBody" id="content">
    </body>
</html>

您可以按如下方式定义模拟服务器:

sap.ui.define([
    "sap/ui/core/util/MockServer"
], function(MockServer) {
    "use strict";
    return {
        /**
         * Initializes the mock server.
         * You can configure the delay with the URL parameter "serverDelay".
         * The local mock data in this folder is returned instead of the real data for testing.
         * @public
         */
        init: function() {
            // create
            var oMockServer = new MockServer({
                rootUri: "/"
            });
            // simulate against the metadata and mock data
            oMockServer.simulate("../localService/metadata.xml", {
                sMockdataBaseUrl: "../localService/mockdata",
                bGenerateMissingMockData: true
            });
            // start
            oMockServer.start();
            jQuery.sap.log.info("Running the app with mock data");
        }
    };
});
  

请确保您将处理自定义网址(包含过滤器/排序)和   函数导入mockserver.js

Read complete steps here