You can use ViewModelName in a ViewModel context when your OCL expression needs the name of the ViewModel that is currently active.
What ViewModelName returns
ViewModelName returns the name of the current ViewModel.
Use it when logic must refer to the active ViewModel name without repeating that name as a text literal. This keeps the expression aligned with the ViewModel when you rename it.
| Situation | Result |
|---|---|
The active ViewModel is named OrderSummaryVM.
|
ViewModelName returns OrderSummaryVM.
|
You rename that ViewModel to OrderSummaryPage.
|
ViewModelName returns OrderSummaryPage.
|
Where you can use it
ViewModelName is available only within the context of a ViewModel. It identifies the ViewModel in which the expression is evaluated; it is not a general-purpose way to supply an arbitrary ViewModel name.
For example, use ViewModelName when diagnostic or other ViewModel-scoped logic needs to report which ViewModel is active. If a user opens OrderSummaryVM, the value is OrderSummaryVM. After the ViewModel is renamed, the same logic uses the new name rather than an outdated hard-coded value.
Why use it
Using ViewModelName helps you:
- Avoid duplicate, hard-coded ViewModel-name strings in ViewModel-scoped logic.
- Keep logic correct when a ViewModel is renamed.
- Make diagnostics identify the active ViewModel.
ViewModel names in other operations
Several OCL operators accept a ViewModel name as an argument. Those operators have their own requirements and syntax:
- Navigate can use an expression for the ViewModel name when conditional navigation is required. Use dynamic navigation cautiously because it makes the application's navigation harder to understand statically.
- canAccess checks access to a supplied ViewModel according to that ViewModel's access expression.
- deepclone, transform, and ApplyTaJson use a ViewModel to define how object data is copied, transformed, or applied.
- ViewModelAsJSon and ViewModelAsXml serialize ViewModel content.
