A module is a code fragment encapsulated in a separate block that is loaded and executed independently. A module has no information about other Creatio modules besides the names of modules on which it depends. Modules can interact with each other via messages. To organize the module interaction, use a sandbox object.
The sandbox object lets you execute the following actions:
- Organize the message exchange among the modules.
- Load and unload modules on request.
Organize the message exchange among the modules
To exchange messages among the modules, Creatio must execute the following actions:
- Register a message.
- Publish a message.
- Subscribe to a message.
A module that needs to inform other Creatio modules about changes to its status publishes a message. A module that needs to receive messages about changes to statuses of other modules subscribes to these messages.
Register a message
You can register a message in the following ways:
- using the sandbox.registerMessages(messageConfig) method
- using a module schema
Register a message using the sandbox.registerMessages(messageConfig) method
The messageConfig parameter is a configuration object that contains module messages. The configuration object is a key-value collection.
- MessageName is the key of the collection item that contains the message name.
-
mode is the message operation mode. Contains the value of the Terrasoft.core.enums.MessageMode enumeration. Learn more about the MessageMode enumeration in the JS class reference.
- Broadcast. The number of subscribers to the message is unknown in advance. Corresponds to the Terrasoft.MessageMode.BROADCAST enumeration value.
- Address. One subscriber handles a message. Corresponds to the Terrasoft.MessageMode.PTP enumeration value. You can specify multiple subscribers, but only one handles the message, usually the last registered subscriber.
-
direction. Message direction. Contains the value of the Terrasoft.core.enums.MessageDirectionType enumeration. Learn more about the MessageDirectionType enumeration in the JS class reference.
- Publish. The module publishes the message to sandbox. Corresponds to the Terrasoft.MessageDirectionType.PUBLISH enumeration value.
- Subscribe. The module subscribes to a message published from another module. Corresponds to the Terrasoft.MessageDirectionType.SUBSCRIBE enumeration value.
- Bidirectional. The module publishes and subscribes to the same message in different instances of the same class or the same schema inheritance hierarchy. Corresponds to the Terrasoft.MessageDirectionType.BIDIRECTIONAL enumeration value.
To cancel the message registration in the module, use the sandbox.unRegisterMessages(messages) method. The messages parameter is the message name or array of message names.
To register a message in a view model, declare a message configuration object in the messages schema property.
Register a message using the module schema
- Open the Configuration section and open a module schema.
-
Add a message to the module schema.
- Click the
button in the context menu of the Messages node.
-
Fill out the message properties.
- Enter the message name in the Name property. The name must match the key in the module configuration object.
-
Select the message direction in the Direction property. Available values:
- Subscribe: subscription to the message
- Publish: message publication
-
Select the message operation mode in the Mode property. Available values:
- Broadcast: broadcast message
- Address: address message
- Click Add to add a message.
- Click the
You do not need to register messages in a view model schema.
Publish a message
To publish a message, use the sandbox.publish(messageName, messageArgs, tags) method.
If the published message contains a tag array, Creatio calls handlers for which one or more tags match. If the published message does not contain a tag array, Creatio calls untagged handlers.
Subscribe to a message
To subscribe to a message, use the sandbox.subscribe(messageName, messageHandler, scope, tags) method.
Load and unload modules on request
Creatio lets you load and unload modules not specified as dependencies when working with UI.
Load a module on request
To load a module on request, use the sandbox.loadModule(moduleName, config) method. Method parameters:
- moduleName is a module name.
- config is a configuration object that contains the module messages. Required for visual modules.
View the examples that call the sandbox.loadModule() method below.
Unload a module on request
To unload a module on request, use the sandbox.unloadModule(id, renderTo, keepAlive) method. Method parameters:
- id is a module ID.
- renderTo is the container name from which to remove the visual module view. Required for visual modules.
- keepAlive indicates whether to save module model. The core can save the model when unloading the module. The saved model lets you use properties, methods, and messages. Not recommended.
View the examples that call the sandbox.unloadModule() method below.
Create a module chain
If you want to display a model view in place of another model view, use a module chain. For example, you can use a module chain to populate a field using a lookup value. To do this, display the module view of the lookup selection page in place of the module container of the current page.
To create a chain, add the keepAlive property to the configuration object of the module to load.
1. Create a module
- Open the Configuration section and select a custom package to add the schema.
-
Click Add → Module on the section list toolbar.
-
Fill out the schema properties in the Module Designer.
- Set Code to "UsrSomeModule."
- Set Title to "SomeModule."
Click Apply to apply the changes.
-
Add the source code in the Module Designer.
- Click Save on the Module Designer’s toolbar.
2. Register a message
- Declare message configuration objects in the messages schema property.
- Add to the init() method the sandbox.registerMessages() method call that registers messages.
3. Publish a message
- Implement the processMessages() method in the module schema.
- In the processMessages() method, call the sandbox.publish() method that publishes the MessageToPublish message.
- Add the processMessages() method call to the init() method.
4. Subscribe to a message
- Add the sandbox.subscribe() method call to the processMessages() method. The sandbox.subscribe() method subscribes to the MessageToSubscribe message sent by another module.
- Specify the onMessageSubscribe() handler method in the method parameters and add it to the module source code.
5. Cancel the message registration
- Set the configuration object as a parameter of the handler function in the module that publishes the message.
-
Add a callback function to the configuration object.
-
Return asynchronous result in the handler method of the subscriber module the module subscribes to a message. Use the callback function parameter of the published message.