You can reuse a rooted ViewModel inside another ViewModel by mounting it at an object-valued column, for example to show data reached through an association without rebuilding the same ViewModel structure.
What a mounted ViewModel does
A mounted ViewModel inserts the definition of one ViewModel into another ViewModel at a mount point. The mounted content is merged into the outer ViewModel where it is mounted.
Conceptually, the result is the same as creating a new nesting from the mount point and repeating the ViewModel classes, attributes, and other structure from the mounted ViewModel. The generated nesting uses the name of the mounted ViewModel.
Use mounting when you want to define a reusable ViewModel for an object and include that definition from multiple outer ViewModels.
Requirements
A mount point must meet both of these conditions:
| Requirement | Meaning |
|---|---|
| Object-valued column | The column in the outer ViewModel must represent an object, not a scalar value such as text, number, or date. |
| No associated ViewModel class | The object-valued column must not already have an associated ViewModel class. |
The ViewModel being mounted should be rooted for instance attributes. A rooted ViewModel has a model object as the context of its root; see ViewModel for the distinction between rooted and unrooted ViewModels.
Example: reuse product details on a category view
Assume that a Category is associated with Product objects and that you have defined a rooted Product ViewModel that exposes the product information you want to display.
Instead of reproducing the Product ViewModel structure in the Category ViewModel, you can mount the Product ViewModel at an object-valued column that supplies the Product data. The outer Category ViewModel then contains the merged Product structure at that location.
This is useful when the same Product presentation is needed from more than one context. For example, a Category view and another view can both use the same mounted Product ViewModel rather than maintaining separate copies of its ViewModel definition.
Use mounting through ViewInView
ViewInView uses the mount strategy to place a nested ViewModel in a view without using the table-grid nested ViewModel approach. It can be used to display data obtained through association links.
To configure this in a view:
- Create the ViewModel that you intend to mount. Root it for instance attributes.
- Add a ViewInView widget to the outer view. You can select it from the options on the right and draw it onto the page; it is also available from the ViewModel context menu.
- Set Column Name to the mount-point column.
- Set the data-loading expression for the mounted ViewModel.
- Set the widget's ViewModel property to the ViewModel to mount.
For the widget-specific configuration and its category/products example, see ViewInView.
Important limitation: vCurrent_Root
Do not use vCurrent_Root inside a mounted ViewModel. The root-current variable belongs to the outer view, so within mounted content it has the value of the outer ViewModel rather than the mounted ViewModel.
For example, if a Product ViewModel is mounted into a Category view, vCurrent_Root in the mounted Product ViewModel refers to the outer Category view's root context, not to the mounted Product context. Design the mounted ViewModel so that it does not depend on vCurrent_Root.
This limitation is especially relevant when defining ViewModel actions, because view actions receive focused items through vCurrent_ variables. Review the action context before reusing an action-oriented ViewModel as mounted content.
When to mount and when to use a normal nesting
| Choose | When |
|---|---|
| Mounted ViewModel | You already have a rooted ViewModel for the object and want to reuse its definition at an eligible object-valued column. |
| Normal ViewModel nesting | You need a ViewModel structure that is specific to this outer ViewModel, or the intended mount point does not meet the mounting requirements. |
| ViewInView | You want to render mounted content within a view and do not want to use the table-grid nested ViewModel approach. |
