Skip to main content
Version: 8.1

define() function

Level: beginner

The purpose of the define() function is to declare an asynchronous module in the source code. The loader works with this module.

Declare the module
define(
ModuleName,
[dependencies],
function (dependencies) {
}
);

Parameters

ModuleName

The string that contains the module name. Optional.

If you do not specify the parameter, the loader assigns a module name automatically based on the module location in the Creatio script tree. The name is required to access the module from other Creatio parts, including asynchronous loading as a dependency of another module.

dependencies

An array of module names on which the current module depends. Optional.

The RequireJS loader loads the module dependencies that are passed as arguments to the define() function. The loader calls the factory function after loading the dependencies listed in the dependencies array.

function(dependencies)

An anonymous factory function that instantiates the module. Required.

Pass the objects that the loader associates with module dependencies as function parameters. List dependencies in the dependencies array. The array arguments are required to access the properties and methods of the dependency modules in the current module. The order of dependencies in the dependencies array corresponds to the order in which the parameters are listed in the factory function.

The factory function returns the value that the loader handles similarly to the exported value of the current module.

The types of values that the factory function returns are as follows:

  • Object. In this case, the object is the module. The browser caches the module after the initial download. If the module declaration changes after the client downloads the module, for example, as part of implementing the configuration logic, clear cache and re-download the module.
  • Module function factory. The constructor receives the scope object as an argument. As a result, downloading a module creates a module instance ( instantiated module) in the client. Re-downloading the module to the client via the require() function creates another module instance. Creatio treats these instances of the same module as individual modules. View the example that declares an instantiated module in the CardModule schema of the NUI package.