SAPUI5 - Mock Server Automated Fallback Config

馋奶兔 提交于 2019-12-25 08:03:15

问题


I am struggling to figure out a solution for a situation when I start my sapui5 application then based on manifest.json configuration application should start mock server when there is no connection for OData else call OData service. Right now I have mockserver.html which start mock server and index.html for application.

Is it even possible what I am asking?

Thanks


回答1:


The mock server intercepts the URL you are specifing at mock server instantiation. Simply check the availability of your OData service and if your service isn't available, you instantiate the mock server.

sap.ui.define([
    "sap/ui/core/util/MockServer",
    "sap/ui/model/odata/v2/ODataModel"
], function(MockServer, ODataModel) {
    "use strict";

    return {
        init: function() {
            var oDataModel = new ODataModel("<your OData URL here>");
            oDataModel.attachMetadataFailed(function() {
                console.log("Metadata load failed :(");

                /* Initialize mock server */
                MockServer.config({
                    autoRespond: true
                });

                var oMockServer = new MockServer({
                    rootUri: "<your OData URL here>"
                });

                oMockServer.simulate("path/to/metadata.xml", {
                    sMockdataBaseUrl: "path/to/mockdata",
                    bGenerateMissingMockData: true
                });

                oMockServer.start();
            });
        }
    };
});

I didn't have any time to test my solution, but it should work this way.



来源:https://stackoverflow.com/questions/41809281/sapui5-mock-server-automated-fallback-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!