You can use ViewModelName in OCL expressions that run in a ViewModel to retrieve the name of the active ViewModel without hard-coding it.
What ViewModelName returns
ViewModelName returns the name of the current ViewModel as text. It is available from the selfVM variable, which represents the ViewModel runtime context.
selfVM.ViewModelNameFor example, when the active ViewModel is named OrderSummaryVM, the expression returns OrderSummaryVM.
Use it from the ViewModel context
Use selfVM.ViewModelName only where selfVM is available: in logic evaluated in the context of a ViewModel. selfVM also exposes operations for working with the current ViewModel, including actions, navigation, refresh operations, and logging.
| Expression | Result when the active ViewModel is OrderSummaryVM
|
|---|---|
selfVM.ViewModelName
|
OrderSummaryVM
|
Avoid hard-coded ViewModel names
Use selfVM.ViewModelName when logic needs to identify the ViewModel that is currently running. This prevents the logic from depending on a string that can become incorrect after a rename.
For example, use the expression when recording which ViewModel a user opened:
selfVM.ViewModelNameIf you later rename OrderSummaryVM to OrderSummaryPage, the expression returns OrderSummaryPage. A hard-coded 'OrderSummaryVM' value would still contain the old name and would need manual maintenance.
When to use it
Use ViewModelName when you need the identity of the active ViewModel at runtime, for example:
- Include the active ViewModel name in diagnostic or logging logic.
- Build logic that must remain correct when the ViewModel is renamed.
- Confirm which ViewModel is active while investigating behavior.
Do not use ViewModelName as a substitute for a static ViewModel reference when defining normal application navigation. For conditional navigation, see Navigate; its use can make the application's navigation harder to understand statically.
Related concepts
A ViewModel defines a perspective on information in your model and can be rooted or unrooted. To create and maintain a ViewModel definition, use the ViewModel Editor. ViewModelName returns the name of the ViewModel currently executing; it does not describe the ViewModel's classes, nestings, or data structure.
