cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between sap.ui.define, sap.ui.require and jQuery.sap.require in sap ui5

alagar_pandian
Participant

Hello,

Can anybody tell me the exact difference between sap.ui.define, sap.ui.require and jQuery.sap.require in sap ui5 with an example.

Regards,

Alagar

Accepted Solutions (1)

Accepted Solutions (1)

boghyon
Product and Topic Expert
Product and Topic Expert
TL;DR:
  • Use sap.ui.define to define a new module.
  • Use sap.ui.require just to require existing module(s), i.e. without defining a new one.
  • jQuery.sap.require: no. Just don't.

______

The following resources provide comprehensive explanations with examples:

After reading those resources carefully, let us know if something is still unclear. I'll expand this answer accordingly.

Answers (2)

Answers (2)

Fabrice
Active Participant

Hi,

in the documentation for sap.ui.define (https://sapui5.netweaver.ondemand.com/1.52.29/#/api/sap.ui/methods/sap.ui.define) there is a part with the difference :

Differences to Standard AMD

The current implementation of sap.ui.define differs from the AMD specification (https://github.com/amdjs/amdjs-api) or from concrete AMD loaders like requireJS in several aspects: The name sap.ui.define is different from the plain define. This has two reasons: first, it avoids the impression that sap.ui.define is an exact implementation of an AMD loader. And second, it allows the coexistence of an AMD loader (e.g. requireJS) and sap.ui.define in one application as long as UI5 or applications using UI5 are not fully prepared to run with an AMD loader. Note that the difference of the API names also implies that the UI5 loader can't be used to load 'real' AMD modules as they expect methods define and require to be available. Modules that use Unified Module Definition (UMD) syntax, can be loaded, but only when no AMD loader is present or when they expose their export also to the global namespace, even when an AMD loader is present (as e.g. jQuery does) sap.ui.define currently loads modules with synchronous XHR calls. This is basically a tribute to the synchronous history of UI5. BUT: synchronous dependency loading and factory execution explicitly it not part of contract of sap.ui.define. To the contrary, it is already clear and planned that asynchronous loading will be implemented, at least as an alternative if not as the only implementation. Also check section Asynchronous Contract above. Applications that need to ensure synchronous loading of dependencies MUST use the old jQuery.sap.require API. sap.ui.define does not support plugins to use other file types, formats or protocols. It is not planned to support this in future sap.ui.define does not support absolute URLs as module names (dependencies) nor does it allow module names that start with a slash. To refer to a module at an absolute URL, a resource root can be registered that points to that URL (or to a prefix of it). sap.ui.define does not support the 'sugar' of requireJS where CommonJS style dependency declarations using sap.ui.require("something") are automagically converted into sap.ui.define dependencies before executing the factory function.

Also, for info:

- JQuery.sap is deprecated since version 1.58 (https://sapui5.hana.ondemand.com/#/api/jQuery.sap)

Regards

Fabrice

ushalekha
Participant

Hi

Use sap.ui.define for controllers and all other JavaScript modules to define a global namespace. With the namespace, the object can be addressed throughout the application.

Use sap.ui.require for asynchronously loading dependencies but without declaring a namespace, for example code that just needs to be executed, but does not need to be called from other code.

Regards

Ushalekha.