|
| 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 | +* BuildinBlocks |
| 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 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 contains the the following fields: |
| 38 | +| Name | Description | |
| 39 | +| --- | --- | |
| 40 | +| `$ID` | The unique id of the unit | |
| 41 | +| `$Type` | The type of the unit | |
| 42 | +| `moduleName` | (Optional) The name of the module containing the unit | |
| 43 | +| `name` | (Optional) The name of the unit | |
| 44 | + |
| 45 | +All the units managed by the DomainModels component can be retrieved by: |
| 46 | + |
| 47 | +```ts |
| 48 | +const unitsInfo = await domainModels.getUnitsInfo() |
| 49 | +``` |
| 50 | + |
| 51 | +Units can be loaded by suppling a function to `loadAll` to execute for each unit. It should return a truthy value to load the unit. |
| 52 | + |
| 53 | +The followind snippet loads the DomainModel for the module named `MyFirstModule`: |
| 54 | + |
| 55 | +{{% alert color="warning" %}} |
| 56 | +Loading units is an expensive process. Only load the minimun number of units you need when you need them. |
| 57 | +{{% /alert %}} |
| 58 | + |
| 59 | +```ts |
| 60 | +const [domainModel] = await domainModels.loadAll((info) => info.moduleName === 'MyFirstModule'); |
| 61 | +``` |
| 62 | + |
| 63 | +## Reading the unit content {#read} |
| 64 | + |
| 65 | +Elements contained inside units can be accessed using the `get<ElementName>` helper methods: |
| 66 | + |
| 67 | +The following snippet will get the Entity named `MyEntity` from the previously loaded DomainModel unit: |
| 68 | + |
| 69 | +```ts |
| 70 | +const entity = domainModel.getEntity("MyEntity"); |
| 71 | +``` |
| 72 | + |
| 73 | +## Modifying the unit content {#modify} |
| 74 | + |
| 75 | +The Mendix model can be modified by leveraing the `add<ElementName>` helper methods: |
| 76 | + |
| 77 | +The following snippet will create a new Entity inside the previously loaded DomainModel unit: |
| 78 | + |
| 79 | +```ts |
| 80 | +const newEntity = await domainModel.addEntity({ name: "NewEntity", attributes: [{ name: "MyAttribute", type: "AutoNumber" }]}) |
| 81 | + |
| 82 | +await domainModels.save(domainModel); |
| 83 | +``` |
| 84 | + |
0 commit comments