1

Does SAPUI5 load the libraries each time I call jQuery.sap.require("someLibrary")? For instance if I am calling the above statement in multiple modules in my application, is "someLibrary" loaded multiple times also?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
  • `jQuery.sap.require` is deprecated. [Use `sap.ui.define` / `sap.ui.require` instead.](https://stackoverflow.com/a/45277948/5846045) – Boghyon Hoffmann Oct 21 '20 at 12:47

4 Answers4

4

In case someone still considers to use jQuery.sap.require, be aware that it sends only synchronous XHRs which should be avoided.

The use of jQuery.sap.require is synchronous and considered as "bad practice" because syncXHR is deprecated by the Web Hypertext Application Technology Working Group. (Source: Modules and Dependencies)

Instead, the current best practice is to use sap.ui.define or .require for asynchronous module loading:

<!-- Enable asynchronous module loading in index.html (available since 1.58.2) -->
<script id="sap-ui-bootstrap" src="..." data-sap-ui-async="true" ...>
sap.ui.define([ // or .require
  // modules to load
], function(/* modules available once loaded */) {
  // ...
});

Asynchronification of module dependency
Source: Asynchronify Your App by Arnd vom Hofe. Note: anonymous sap.ui.define can be called only once at top-level.

Same as jQuery.sap.require, the API sap.ui.require([/*...*/]) fetches the modules also only once depending on their internal states.

For more information, please take a look at the topics under Modules and Dependencies.


Sync XHR is deprecated not only by the web platform generally, but will be also replaced by UI5 gradually with a new set of APIs.

Deprecation of Sync XHR
Source: UI5 Evolution by Peter Muessig

Consequently, jQuery.sap.require and jQuery.sap.declare are now deprecated and will be removed in the next major UI5 version (aka. "Legacy-free UI5")!

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
2

The lib is only loaded once. You can find this information in the SDK https://sapui5.hana.ondemand.com/sdk/#docs/guide/ModularizationConcept.html

Module Loading

As mentioned already, modules are loaded by calling function jQuery.sap.require with the name of a required module. The framework then checks whether the named module is loaded already. If so, the function simply returns. Otherwise it tries to load and execute the module synchronously. If any of these two steps fails, an exception is thrown and execution of the calling module thereby is disrupted.

Community
  • 1
  • 1
cevou
  • 325
  • 1
  • 9
  • so, it is ok to load the libraries winthin the application.js file (as described in the best practices recommendation) would that boost the user experience since now I have some pages in my application that require many modules and when the user clicks on the tile (to access the page) the app halts for a second! – Mohamed Ali JAMAOUI Oct 08 '13 at 13:33
1

The libraries are loaded once. This can be seen in the network tab in chrome developer tools.

Also check the documentation as pointed by cevou here:

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
1

When you call this function with some library, it checks that given library is loaded or not using associative array. If the library is loaded, then it returns null. And if the library is not loaded, then it loads the library using sjax call and after a success of the sjax call, it sets the library name as key into associative array.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Prashant Gurav
  • 505
  • 7
  • 17