Commands and Operations
When writing custom components you may want to take advantage of Geocortex Web's large built-in suite of command and operations or your own custom commands and operations.
Running Commands and Operations
Commands and operations can be run in any custom service or component. Components can run commands and operations in two ways; through the component model, or through the React component.
tip
It is best practice to run commands and operations through the components model, and express the result through a change in the models data rendered by the component.
Command and Operations in a Model
Commands and operations can be run through the messages
property of the model.
Command and Operations in a Component
Commands and operations can be run through the UIContext
of a component.
Commands and Operations in a Service
Services can run commands and operations through the messages
property.
Implementing Commands and Operations
Components and services can also implement commands and operations. Command and operation implementations are defined in a component's model or in a service. By decorating a method with @command
or @operation
, components and services can define an implementation for a command or operation. The command or operation must also be registered through the registerOperation
or registerCommand
methods in the library registry with the same item type of the model or the ID of the service.
The following example demonstrates a component model defines an implementation for a custom command that takes a custom argument type, and a service that implements a custom operation that returns a number.
note
It's best practice to expose the arguments and return values for commands and operations as public interfaces, as this allows consumers of the command or operation to have the appropriate typing.
- Component Model
- Service
- Registration
@command
and @operation
Decorator Options
Both the @command
and @operation
decorator functions can take an optional second argument specifying parameters that affect the behavior of the command or operation.
The targetInactive
option applies to commands and operations implemented through a component model. The default value is false. If targetInactive
is set to true, the command or operation will be executed even if the associated component is not currently active in the layout
The parallel
option applies to commands only. The default value is false. By default, if a command has multiple implementations, each implementation is executed sequentially in the order it was registered in the library, and multiple component model implementations are executed in layout order. Setting parallel
to true causes the command implementation to be run in parallel with other implementations marked as parallel
.
note
If there is a mix of parallel and sequential command implementations, then the parallel implementations will run concurrently after all sequential implementations have finished executing.
canExecute
Hook
Commands and services can optionally implement a hook function which indicates whether it is possible to execute the command. This is accomplished by implementing a "canExecute" method in the same class as the command implementation. This method is decorated with @canExecute
and returns a boolean indicating whether the command can or cannot run based on the given argument.
If the canExecute
hook for the command implementation returns true, the execute
hook will be called. If the function returns false, the execute
hook will not be called. Some components, like the I Want To Menu, use the canExecute
hook to disable buttons when no implementations of a command can be executed. This is accomplished through the canExecuteChanged
event. This event can be published or subscribed to in order to react to changing command execution conditions.
tip
For a full example of a command with a dynamic execution status, check out the canExecute
tutorial, or this live SDK sample.
Running Custom Commands
You can run the custom commands or operations you define from another component, model, or service by locating them by ID and providing the appropriate type arguments.