SAPUI5 - Mock Server Automated Fallback Config

2019-09-09 11: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.



标签: sapui5