|
| 1 | +--- |
| 2 | +title: "Model Api" |
| 3 | +url: /apidocs-mxsdk/apidocs/extensibility-api/web/model-api/ |
| 4 | +weight: 6 |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +This how-to provides guidance on using the Model Access API: |
| 10 | + |
| 11 | +* [Using the Model Access API](#using-api) |
| 12 | +* [Reading the units info and loading units](#units-info-load) |
| 13 | +* [Reading the unit content](#read) |
| 14 | +* [Modifying the unit content](#modify) |
| 15 | + |
| 16 | +## Using the Model Access API {#using-api} |
| 17 | + |
| 18 | +The Model Access API allow access to the Mendix model. |
| 19 | + |
| 20 | +The model is split in several components exposed via `studioPro.app.model` object. Currently supported components are: |
| 21 | +* BuildingBlocks |
| 22 | +* DomainModels |
| 23 | +* Enumerations |
| 24 | +* Pages |
| 25 | +* Snippets |
| 26 | + |
| 27 | +```ts |
| 28 | +const { pages, domainModels } = studioPro.app.model; |
| 29 | +``` |
| 30 | + |
| 31 | +## Reading the units info and loading units {#units-info-load} |
| 32 | + |
| 33 | +An element is part of a Mendix model and all elements together form the logic of the model. Elements may contain other elements. An element always has a container element, which is its parent. The root of an element tree is always a unit. |
| 34 | + |
| 35 | +Each component (e.g. `studioPro.app.model.pages` and `studioPro.app.model.domainModels`) exposes the units info of the units it is responsible for. The full unit content can be accessed only after loading the unit. |
| 36 | + |
| 37 | +The unit info, described by the `UnitInfo` interface, contains the the following fields: |
| 38 | +| Name | Description | Example value | |
| 39 | +| --- | --- | --- | |
| 40 | +| `$ID` | The unique id of the unit | `077d1338-a548-49a9-baee-c291e93d19af` | |
| 41 | +| `$Type` | The type of the unit | `Pages$Page` | |
| 42 | +| `moduleName` | (Optional) The name of the module containing the unit | `MyFirstModule` | |
| 43 | +| `name` | (Optional) The name of the unit | `ExamplePage` | |
| 44 | + |
| 45 | +All the units managed by the DomainModels component can be retrieved by: |
| 46 | + |
| 47 | +```ts |
| 48 | +const unitsInfo: Primitives.UnitInfo[] = await domainModels.getUnitsInfo() |
| 49 | +``` |
| 50 | + |
| 51 | +Units can be loaded by supplying a function to `component.loadAll(fn)` to execute for each unit. The function `fn` should return a truthy value to load the specified unit. |
| 52 | + |
| 53 | +The followind snippet loads the DomainModel for the module named `MyFirstModule`: |
| 54 | + |
| 55 | +{{% alert color="warning" %}} |
| 56 | +Loading units is a resource intensive process. Only load the minimum number of units you need when you need them. |
| 57 | +{{% /alert %}} |
| 58 | + |
| 59 | +```ts |
| 60 | +const [domainModel] = await domainModels.loadAll((info: Primitives.UnitInfo) => info.moduleName === 'MyFirstModule'); |
| 61 | +``` |
| 62 | +The followind snippet loads the Page named `Home_Web` inside the module named `MyFirstModule`: |
| 63 | + |
| 64 | +```ts |
| 65 | +const [page] = await pages.loadAll((info: Primitives.UnitInfo) => info.moduleName === 'MyFirstModule' && info.name === 'Home_Web') |
| 66 | +``` |
| 67 | +## Reading the unit content {#read} |
| 68 | + |
| 69 | +Elements contained inside units can be accessed using the `get<ElementName>` helper methods. |
| 70 | + |
| 71 | +The following snippet will get the Entity named `MyEntity` from the previously loaded DomainModel unit: |
| 72 | + |
| 73 | +```ts |
| 74 | +const entity: DomainModels.Entity = domainModel.getEntity("MyEntity"); |
| 75 | +``` |
| 76 | + |
| 77 | +## Modifying the unit content {#modify} |
| 78 | + |
| 79 | +The Mendix model can be modified by leveraging the `add<ElementName>` helper methods. |
| 80 | + |
| 81 | +The following snippet will create a new Entity inside the previously loaded DomainModel unit: |
| 82 | + |
| 83 | +{{% alert color="warning" %}} |
| 84 | +Do not forget to invoke the `component.save(unit)` method after making changes to your unit. The method must be invoked for each modified unit, so changes on multiple units need to be saved separetely. |
| 85 | +{{% /alert %}} |
| 86 | + |
| 87 | +```ts |
| 88 | +const newEntity: DomainModels.Entity = await domainModel.addEntity({ name: "NewEntity", attributes: [{ name: "MyAttribute", type: "AutoNumber" }]}); |
| 89 | + |
| 90 | +newEntity.documentation = "New documentation"; |
| 91 | + |
| 92 | +await domainModels.save(domainModel); |
| 93 | +``` |
| 94 | + |
0 commit comments