Search code examples
sapui5sap-fiori

Access controller of another application in SAPUI5


I have some SAPUI5 applications in one SAP server. All the applications will be deployed in an ERP Server Version 6.0.

All the applications has a access url in our ERP server that can be retrieved through the SAP NetWeaver. All the urls have the format commonPart/applicationName/index.html. All of these application will be accessible through a Fiori launchpad application.

Now I have a controller that has to be shared between all my applications. Is there any method that I can share this controller without copy and past?

Thanks in advance


Solution

  • You can create a reusable library project for SAP Fiori applications. Put your BaseController in that library. Then import the library in your application project and extend your application controller from BaseController.

    Define library.js in root folder of your base project

    sap.ui.define(["jquery.sap.global",
                "sap/ui/core/library"], // library dependency
    function(jQuery) {
    
        "use strict";
        // delegate further initialization of this library to the Core
        sap.ui.getCore().initLibrary({
            name: "mylibrary.reuse",
            version: "1.0",
            dependencies: ["sap.ui.core"],
            types: [],
            interfaces: [],
            controls: [],
            elements: [],
            noLibraryCSS: true
        });
    
        return mylibrary.reuse;
    
    }, /* bExport= */ true);
    

    Declare the BaseController within it's JS file

     jQuery.sap.declare("mylibrary.reuse.BaseController");
     /** Controller Definition **/
    

    Define and expose entry points to your library in neo-app.json

     {
       "routes": [{
        "path": "/resources/mylibrary/reuse",
        "target": {
            "type": "application",
            "name": "myreuselibrary",
            "entryPath": "/mylibrary/reuse"
        },
        "description": "SAPUI5 Resources"
    },
    

    }

    Then import the above library in application project, do JQuery.require() got BaseController and then extend it.

    I hope this helps.