Adding dynamic connections
The Cubbles Javascript API offers a method called addDynamicConnection
, which allows adding a connection between two components dynamically, which should be within the same Context. The argument of this method is an instance of the window.cubx.cif.DynamicConnection
object, which have the following properties:
Property name
Type
Requirement
Description
source
object
required
Represents the source of the connection. It has the following properties: - runtimeId (string): indicates the runtimeId of the source component - slot (string): indicates the name of the source slot of the connection
destination
object
required
Represents the target or destination of the connection. It has the following properties: - runtimeId (string): indicating the runtimeId of the destination component - slot (string): indicating the name of the destination slot of the connection
connectionId
string
optional (automatically generated)
Indicates the id for this connection, which should be unique within a context
hookFunction
string
optional
Describes a function or the name of a function to be called before a value is set in a destination slot
repeatedValues
boolean
optional
Indicates whether a same value can be propagated consecutively
copyValue
boolean
optional
Indicates whether the payload of a connection should be copied or not
directExecution
boolean
optional
Indicates whether the connection should start working after its creation
Public methods
To set and validate the properties of a DynamicConnection
you can use the following methods:
Method name
Description
setSource (source)
Sets the source object of a dynamic connection
setSourceRuntimeId (runtimeId)
Sets the runtimeId property of the source of a dynamic connection
setSourceSlot (slotName)
Sets the slot name of the source of a dynamic connection
setDestination (destination)
Sets the destination object of a dynamic connection
setDestinationRuntimeId (runtimeId)
Sets the runtimeId of the destination of a dynamic connection
setDestinationSlot (slotName)
Sets the slot name of the destination of a dynamic connection
setConnectionId (connectionId)
Sets an id to the connection Note: when you add a new dynamic connection its connectionId will be generated automatically; thus, you don't need to set it.
setCopyValue (copyValue)
Sets the copyValue property of a connection, which indicates whether the payload of the connection should be copied or not
setRepeatedValues (repeatedValues)
Sets the repeatedValues property of a connection, which indicates whether the same value can be propagated consecutively
setHookFunction (hookFunction)
Sets a function to a connection, which will be called before the value is set to the destination slot
setDirectExecution (directExecution)
Sets the directExecution property of a dynamic connection, which indicates whether the connection should start working after its creation; i.e., the current value of the source should be propagated immediately.
validate ()
Validates all the properties of a dynamic connection. Additionally, it throws and reports errors (if any)
Creating and adding a dynamic connection
You can create a dynamic connection to add it to an existing component using the methods described above. To create a connection, first, you need to access the source and destination component:
Then, you should instantiate and set the properties of a DynamicConnection object:
Finally, you need to access the desired component (source or destination) and add the dynamic connection:
A working example
In this demo, we have two instances of our cubx-textarea
component and will connect their value slots. The first cubx-textarea
will be the source component, which will have an initial value to see the effect of having the directExecution property set to true. The code below allows to:
Get the source and destination components from DOM
Listen to the 'cifReady' event to enable the 'Add dynamic connection' button
Listen to the 'click' event
Instantiate and set the properties of the DynamicConnection object
Add the DynamicConnection to the source component
Disable the 'Add dynamic connection' button
Code
Result
Check this demo to see the result working online.
Last updated