When to use jQuery.sap.registerModulePath() with an example?

后端 未结 1 1672
情深已故
情深已故 2021-01-06 06:00

I have tried reading the SAPUI5 documentation for the above but I am not able to clearly understand its usage. Also what is the difference between sap.ui.localResource

1条回答
  •  [愿得一人]
    2021-01-06 06:29

    If you were using resourceRoots either in the bootstrap config or in the app descriptor, you've been using jQuery.sap.registerModulePath all the time as each key-value pair, defined in resourceRoots, is passed as arguments to that static method.

    For example, you may have something like this in your index.html:

    
    

    UI5 then registers the namespace ("my.app") globally as a reference saying "Whenever that name is mentioned while resolving other module names, I should look for the target module in the registered path" which is "./" relative to the current document.location.href in our case.

    The above code is the same as calling jQuery.sap.registerModulePath("my.app", "./")[1] directly.

    • Here, we can pass user-defined URL prefix. E.g.: "../" instead of "./" which is needed if the project has another *.html file in one hierarchy level deeper such as mockserver.html
    • If we've a module in a deeper hierarchy, e.g. in custom/control/somewhere/c3/chart/, we can register another namespace: "my.app.c3chart": "./custom/control/somewhere/c3/chart" and then
      • Use it as a shortcut for example in the XMLView definition:
        xmlns:c3="my.app.c3chart" instead of xmlns:c3="my.app.custom.control.somewhere.c3.chart".
      • Reduce maintenance costs by changing only the registered path when the module file changes its hierarchy level. The namespace "my.app.c3chart" can still be used everywhere.

    [1]: As of 1.58, the API is deprecated. When registering namespaces manually, the API sap.ui.loader.config should be used:

    sap.ui.loader.config({
      paths: {
        "my/anotherApp": "https://example.com/somePath/anotherApp"
      }
    });
    

    What is the difference between sap.ui.localResources() and jQuery.sap.registerModulePath()?

    Here is the current source code of what sap.ui.localResources actually does:

    sap.ui.localResources = function(sNamespace) {
      jQuery.sap.registerModulePath(sNamespace, "./" + sNamespace.replace(/\./g, "/"));
    };
    

    That's it. It calls jQuery.sap.registerModulePath right away with the dots in the namespace (if any) replaced with "/".

    • We can't pass any user-defined URL prefix
    • The namespace is tightly coupled with the actual folder hierarchy and name.
    • SAP discourages the usage of this API (for non-stand-alone apps).
    • The API reference mentions that this API will become obsolete. is deprecated

    0 讨论(0)
提交回复
热议问题