You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [GeneratedC# DTOs](/stacks/agnostic/dtos.md) implementthenecessarylogicforthis; however, any [CustomDTOs](/modeling/model-types/dtos.md) musthavethislogicmanuallywrittenbyyou, thedeveloper. Eitherimplementthesamepatternthatcanbeseeninthe [GeneratedC# DTOs](/stacks/agnostic/dtos.md), ordonotusesurgicalsaveswithCustomDTOs.
Copy file name to clipboardExpand all lines: docs/stacks/vue/layers/viewmodels.md
+31-25Lines changed: 31 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
-
# ViewModels
1
+
# View Models
2
2
3
3
<!-- MARKER:summary -->
4
4
5
-
Coalesce's generated TypeScript ViewModels provide rich, stateful wrappers around your data modeltypes that handle common UI concerns like loading, saving, validation, and auto-save functionality. They serve as the primary interface between your Vue components and your backend APIs, offering features like automatic dirty tracking on property changes, API callers with loading/error states, and transactional bulk save operations.
5
+
Coalesce's generated TypeScript ViewModels provide rich, stateful wrappers around your [CRUD Models](/modeling/model-types/crud.md) and [Services](/modeling/model-types/services.md) that handle common UI concerns like loading, saving, validation, and auto-save functionality. They serve as the primary interface between your Vue components and your backend APIs, offering features like automatic dirty tracking on property changes, API callers with loading/error states, and transactional bulk save operations.
6
6
7
-
The ViewModels are generated as `viewmodels.g.ts` and export a ViewModel class for each API-backed type in your data model ([CRUD Models](/modeling/model-types/crud.md) and [Services](/modeling/model-types/services.md)), as well as a ListViewModel type for [CRUD Models](/modeling/model-types/crud.md).
7
+
The ViewModels are generated as `viewmodels.g.ts` and export the following:
8
8
9
-
These classes provide a wide array of functionality that is useful when interacting with your data model through a user interface. The generated ViewModels are the primary way that Coalesce is used when developing a Vue application.
9
+
- A [ViewModel](#viewmodels) type for each [CRUD Model](/modeling/model-types/crud.md).
10
+
- A [ListViewModel](#listviewmodels) type for each [CRUD Model](/modeling/model-types/crud.md).
11
+
- A [ServiceViewModel](#service-viewmodels) for each [Service](/modeling/model-types/services.md).
10
12
11
13
<!-- MARKER:summary-end -->
12
14
@@ -22,34 +24,38 @@ Changing the value of a property will automatically flag that property as dirty.
22
24
23
25
There are a few special behaviors when assigning to different kinds of data properties on View Models as well:
24
26
25
-
#### Model Object Properties
26
-
- If the object being assigned to the property is not a ViewModel instance, a new instance will be created automatically and used instead of the incoming object.
27
-
- If the model property is a reference navigation, the corresponding foreign key property will automatically be set to the primary key of that object. If the incoming value was null, the foreign key will be set to null.
28
-
- If deep auto-saves are enabled on the instance being assigned to, auto-save will be spread to the incoming object, and to all other objects reachable from that object.
27
+
-**Model Object Properties**
29
28
30
-
#### Model Collection Properties
31
-
- When assigning an entire array, any items in the array that are not a ViewModel instance will have an instance created for them.
32
-
- The same rule goes for pushing items into the existing array for a model collection - a new ViewModel instance will be created and used instead of the object(s) being pushed.
29
+
If the object being assigned to the property is not a ViewModel instance, a new instance will be created and used instead of the incoming object.
30
+
If model property is a reference navigation, the corresponding foreign key property will be set to the primary key of that object.
31
+
32
+
-**Model Collection Properties**
33
+
34
+
When assigning an entire array, any items in the array that are not a ViewModel instance will have an instance created for them.
35
+
The same rule goes for pushing items into the existing array for a model collection - a new ViewModel instance will be created and used instead of the object(s) being pushed.
33
36
34
-
#### Foreign Key Properties
35
-
- If the corresponding navigation property contains an object, and that object's primary key doesn't match the new foreign key value being assigned, the navigation property will be set to null.
37
+
-**Foreign Key Properties**
36
38
39
+
If the corresponding navigation property contains an object, and that object's primary key doesn't match the new foreign key value being assigned, the navigation property will be set to null.
37
40
38
41
39
-
### Other Generated Members
42
+
In addition to the getters and setters for the model's basic data properties, the following are also generated:
40
43
41
-
#### API Callers
42
-
- For each of the instance [Methods](/modeling/model-components/methods.md) of the type, an [API Caller](/stacks/vue/layers/api-clients.md#api-callers) will be generated.
44
+
-**API Callers**
43
45
44
-
#### `addTo*()` Functions
45
-
- For each [collection navigation property](/modeling/model-components/properties.md), a method is generated that will create a new instance of the ViewModel for the collected type, add it to the collection, and then return the new object.
46
+
For each of the instance [Methods](/modeling/model-components/methods.md) of the type, an [API Caller](/stacks/vue/layers/api-clients.md#api-callers) will be generated.
47
+
48
+
-**`addTo*()` Functions**
49
+
50
+
For each [collection navigation property](/modeling/model-components/properties.md), a method is generated that will create a new instance of the ViewModel for the collected type, add it to the collection, and then return the new object.
46
51
47
-
#### Many-to-many helper collections
48
-
- For each [[ManyToMany]](/modeling/model-components/attributes/many-to-many.md)[collection navigation property](/modeling/model-components/properties.md), a getter-only property is generated that returns a collection of the object on the far side of the many-to-many relationship. Nulls are filtered from this collection.
52
+
-**Many-to-many helper collections**
53
+
54
+
For each [[ManyToMany]](/modeling/model-components/attributes/many-to-many.md)[collection navigation property](/modeling/model-components/properties.md), a getter-only property is generated that returns a collection of the object on the far side of the many-to-many relationship. Nulls are filtered from this collection.
Loads data from the provided model into the current ViewModel, and then clears all dirty flags.
111
117
@@ -116,11 +122,11 @@ If auto-save is enabled, only non-dirty properties are updated. This prevents us
116
122
If `purgeUnsaved` is true, items without a primary key will be dropped from collection navigation properties. This is used by the `$load` caller in order to fully reset the object graph with the state from the server.
Same as `$loadCleanData`, but does not clear any existing dirty flags, nor does it clear any dirty flags that will be set while mutating the data properties of any ViewModel instance that gets loaded.
0 commit comments