Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sapui5

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.localResources() and jQuery.sap.registerModulePath() and when to use what?

If someone can explain with an easy example it will be really helpful. Also can we use jQuery.sap.registerModulePath() to load mockData?

like image 581
loki Avatar asked Dec 23 '22 14:12

loki


1 Answers

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:

<script id="sap-ui-bootstrap" src="..."
  data-sap-ui-resourceroots='{"my.app": "./"}'
  ...
></script>

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
like image 142
Boghyon Hoffmann Avatar answered Apr 08 '23 10:04

Boghyon Hoffmann