You can define a class action in MDriven Designer when an operation belongs to an object in the model and must be available wherever that class is shown.
A class action is an action whose context is an instance of one class. It works from the model object itself, rather than from the state of a particular ViewModel. For example, a ShowDetails action on Person can be made available in every view that presents a Person.
When to use a class action
Use a class action when the action:
- operates on the current object of a class;
- does not need the current selection, focus, or other state of one specific ViewModel; and
- should be reusable wherever the class is exposed in the user interface.
For example, put ShowDetails on Person when it acts on one Person. Do not make a separate copy of the action for each ViewModel that shows people.
| Action type | Context available to the action | Use it when |
|---|---|---|
| Class action | The object instance of its owning class, available as self.
|
The operation belongs to the model object and should be reusable across views. |
| ViewModel action | ViewModel state, including vCurrent_ and vSelected_ variables for relevant ViewModel nestings.
|
The operation depends on what is focused, selected, or otherwise specific to one view. |
Context and expressions
A class action always has an instance of its owning class as its root context. In the action's Enable Expression and Execute Expression, self refers to that instance.
For example, when an action belongs to SysSuperClass, self has SysSuperClass as its context. Write expressions that navigate from self through the model associations and attributes needed by the action.
Class actions cannot use the ViewModel variables vCurrent_ or vSelected_. Those variables describe a particular view and are available to ViewModel actions, not class actions.
The Enable Expression determines whether the action is enabled for the current object. The Execute Expression defines what happens when the action runs. These expressions use OCL and action behavior as configured in the action definition.
Create a class action
In MDriven Designer, create the action from the class that owns the behavior.
- Open the model diagram containing the class.
- Right-click the class header.
- Select Add Class action.
- Enter an action name.
- Define the Enable Expression if the action should only be available in a particular object state.
- Define the Execute Expression or select the applicable standard action behavior in the action definition.
- Save the model.
You can also display a class's existing class actions by selecting the small arrow triangle at the top of the class in the diagram view. From that display, you can create actions and define drag actions.
Name actions for readable presentation
Name class actions in camel case. MDriven splits camel-cased names into words for presentation until you choose to change the presentation.
For example:
| Action name | Initial displayed name |
|---|---|
ShowCustomerDetails
|
Show Customer Details |
CreateInvoice
|
Create Invoice |
Use names that describe what the user can do. For example, prefer ShowCustomerDetails over DoIt.
Availability in views
Class actions are globally available to views that show objects of the owning class, subject to the ViewModel's type context and action inclusion settings. This lets one action follow an object type through the application instead of being recreated for each view.
For example, when a ViewModel nesting is typed as Person, actions defined for Person can appear there. If that nesting is changed to Car, the Person actions no longer match that type context and actions for Car can appear instead.
You can control whether a matching class action is included at a ViewModel location:
- In the action definition, review the ViewModel places whose type information matches the action.
- Use the opt-in setting to control inclusion at those matching levels.
- In the ViewModel tree, use the arrow in front of a class action to opt it in or out at that location.
See Opting out actions for the opt-in and opt-out workflow.
Sometimes a grid needs to show objects of class B, while the relevant actions belong to a related class A. Instead of duplicating the actions on B, configure Act As For Actions in the ViewModel nesting with an expression that navigates from B to zero or one A object.
For example, if B has an association to A, use self.A when that expression results in at most one A. The context remains B, but MDriven can expose the actions of A. See Act As For Actions for the full pattern.
Drag actions
A drag action is a class action that responds when one on-screen object is dragged onto another object. Define drag actions from the class-actions display for the relevant class. Use a drag action only when the interaction is based on the participating model objects; use a ViewModel action when it requires view-specific state.
Class actions in tables
Class actions can be placed in a table on a screen. Their enabled or disabled state works as it does elsewhere in the user interface. For the table-specific behavior and the limitation for ViewModel/context actions, see Web client actions in tables.
