How does jQuery.sap.require work?

前端 未结 4 1636
囚心锁ツ
囚心锁ツ 2021-01-07 11:22

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

相关标签:
4条回答
  • 2021-01-07 11:58

    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.

    0 讨论(0)
  • 2021-01-07 12:02

    In case someone still considers to use jQuery.sap.require, be aware that it sends only sjax requests 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:

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


    Source: Asynchronify Your App by Arnd vom Hofe

    Same as jQuery.sap.require, sap.ui.require also loads module only once since both APIs call the same function named requireModule internally in which the module gets loaded depending on its state.


    Sync XHRs is deprecated not only by the web platform generally, but will be replaced by UI5 gradually with new APIs.


    source: UI5 Evolution by Peter Müßig

    Consequently, jQuery.sap.require and jQuery.sap.declare are now deprecated.

    0 讨论(0)
  • 2021-01-07 12:13

    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:

    • https://openui5.hana.ondemand.com/#/topic/91f23a736f4d1014b6dd926db0e91070
    0 讨论(0)
  • 2021-01-07 12:21

    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.

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