Remote and local OData service

2019-09-18 17:34发布

How can I configure manifest.json file so when I run mockserver(mockserver.html) then it goes to the local json data and when I run index.html (main entry to application) then it goes to the remote service. I have a sample manifest.json file from the documentation but not very clear how remote and local service come into play.

{
"_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"
        }
}

}

Controller code

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

Thanks for the help.

标签: sapui5
1条回答
走好不送
2楼-- · 2019-09-18 17:51

You don't need to touch manifest.json file for mocking the service. In fact, in manifest.json the property of dataSources- localUri: takes relative URL to local metadata document or annotation uri. Not for the service.

I hope your mockserver.html looks like this:

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

You can define your mock server as below:

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

Please make sure you will handle custom urls (with filters/sorts) and function import properly in mockserver.js.

Read complete steps here

查看更多
登录 后发表回答