selfVM.DirtyList lets you detect unsaved changes in the current ViewModel so that you can save, inspect, or discard those changes from an OCL expression.
What DirtyList returns
selfVM.DirtyList returns the collection of objects that are dirty in the current ViewModel or OCL execution scope. A dirty object has been changed but has not yet been saved.
An object becomes dirty when you:
- Change an attribute.
- Add or remove a role connection.
- Create a new object.
For example, if a user changes a Person's Name in a ViewModel, that Person is included in selfVM.DirtyList until the changes are saved or discarded.
Use DirtyList in expressions
Use DirtyList where an expression needs to react to pending changes.
| Goal | Expression | Example use |
|---|---|---|
| Get the changed objects | selfVM.DirtyList
|
Pass the changed objects to another OCL operation. |
| Count changed objects | selfVM.DirtyList->size
|
Display or evaluate the number of pending changes. |
| Disable an action when there are no changes | selfVM.DirtyList->isEmpty
|
Use as a Save action's DisableExpression. |
| Show an action only when changes exist | selfVM.DirtyList->notEmpty
|
Use as a Save action's VisibleExpression. |
Add a Save action that follows the dirty state
In the ViewModel Editor, you can use DirtyList to prevent users from selecting Save when there is nothing to save.
- Add a ViewModel column for the action and name it
Save. - Set its Expression to
selfVM.Save. - Set the Save column's DisableExpression to
selfVM.DirtyList->isEmpty. - Save and test the ViewModel.
- Change a value, such as a person's name. The Save action becomes enabled because DirtyList is no longer empty.
- Save the change. The Save action becomes disabled again when there are no remaining dirty objects.
You can also set the VisibleExpression to selfVM.DirtyList->notEmpty when the action should be hidden until changes exist. If the UI configuration supports following the enable state for visibility, use the FollowEnable setting instead of maintaining both expressions.
Discard pending changes
To revert all changes represented by the current DirtyList, use DiscardChanges:
selfVM.DirtyList->DiscardChangesDiscardChanges iterates over the objects in the collection and restores their last known state from the persistence store. This reverses pending attribute edits, additions, and deletions for those objects. Use it only when the user intends to abandon the unsaved work.
For example, a Cancel action can use the expression above to undo a changed Name and remove a newly created, unsaved related object from the pending changes.
DirtyList and saving
Use selfVM.Save to save pending ViewModel changes. DirtyList identifies the objects affected by those pending changes; it does not save them by itself.
If you need to determine whether one particular attribute has a pending change rather than whether an object is in DirtyList, use IsDirtyMember.
