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
## 1. Load a Resource from the Backend into the Store
117
-
To load a resource from the backend into the NgRx Signal Store you can use the `withHypermediaResource` feature from **ngrx-hateoas**. This feature adds state, metastate and methods to the store to work with this resource. The following snippet shows how to set up a signal store for the flight resource:
117
+
To load a resource from the backend into the NgRx Signal Store you can use the `withHypermediaResource(<ModelName>, <InitialState>)` feature from **ngrx-hateoas**. This feature adds state, metastate and methods to the store to work with this resource. The following snippet shows how to set up a signal store for the flight resource:
The store can be injected in your Angular component, a router activator or a router resolver. The `withHypermediaResource`add a method called `load<ModelName>FromUrl` which load the resource from the backend into the store. In our case the model name is `flightModel`. Also a property called`flightModel` is added to the store which holds the current state of the resource in form a NgRx deep signal. The following code snippet shows how to inject the store, load data from the backend and access the singals holding the data;
129
+
The store can be injected in your Angular component, a router activator or a router resolver. The `withHypermediaResource`adds a method called `load<ModelName>FromUrl` which loads the resource from the backend into the store. In our case the model name is `flightModel`. Also a property named with the provided `<ModelName>`, in our case`flightModel` is added to the store which holds the current state of the resource in form a NgRx deep signal. The following code snippet shows how to inject the store, load data from the backend and access the singals holding the data;
130
130
131
131
```ts
132
132
// Inject the store using Angular's inject function
133
133
store=inject(FlightEditStore);
134
134
135
135
// Load the flight resource from the backend into the store via an async call
The `withHypermediaResource` feature also adds a property called `<modelName>State` to the store which holds metastate information about the resource. This can be used to e.g. show loading spinners in the UI while the resource is loaded from the backend. The following code snippet shows how to access some of the metastate signals:
143
+
The `withHypermediaResource` feature also adds a property called `<ModelName>State` to the store which holds metastate information about the resource. This can be used to e.g. show loading spinners in the UI while the resource is loaded from the backend. The following code snippet shows how to access some of the metastate signals:
144
144
145
145
```ts
146
146
// A signal toggling between true and false to indicate if the
You can now set a new state of the connection object by using the `writableFlightConnection` signal on the store like this: `store.writableFlightConnection.set(<new value>)`.
196
196
197
+
:::info
198
+
The store properties created by the `withWritableStateCopy` feature are writable signals and are deep signals at the same time. Therefore you can get fine grained signals from the state copy the same way as you get them from the store state itself.
199
+
:::
200
+
197
201
### Option 3: Use a Deep Writable State Copy
198
-
In case you want want to bind your state to a template driven form you can use the `withExperimentalDeepWritableStateCopy` feature from **ngrx-hateoas**. While `withWritableStateCopy` creates a writable signal for the root object only, the `withExperimentalDeepWritableStateCopy` feature creates a deep writable signal which allows you to set each key of the object individually. This is especiall helpfull in cases where you want to bind your data to a form with the help of `ngModel`. The following code snippet shows how to set up the store with this feature:
202
+
While `withWritableStateCopy` creates a writable signal for the root object only, the `withExperimentalDeepWritableStateCopy` feature creates a deep writable signal which allows you to set each key of the object individually. This is especiall helpfull in cases where you want to bind your data to a form with the help of `ngModel`. The following code snippet shows how to set up the store with this feature:
You can now set a new state of the connection object by using the `writableFlightConnection` signal on the store like this: `store.writableFlightConnection.from.set('<new value>')`.
218
+
You can now set a new state of individual prperties of the connection object by using the `writableFlightConnection` signal on the store like this: `store.writableFlightConnection.from.set('<new value>')`.
215
219
216
220
:::info
217
221
This feature is currently experimental and might change in future releases.
218
222
:::
219
223
220
224
### Option 4: Use a Deep Writable State Delegate
221
-
If you want to have a writalbe signal in the interface of your store which directly modifies the state in the store you can use the `withExperimentalDeepWritableStateDelegate` feature from **ngrx-hateoas**. This feature creates a writable signal graph which directly modifies the state in the store when you call `set` on it. Because it is a deep writable it works well with template driven forms as well as signal firms. The following code snippet shows how to set up the store with this feature:
225
+
If you want to have a writalbe signal in the interface of your store which directly modifies the state in the store you can use the `withExperimentalDeepWritableStateDelegate` feature from **ngrx-hateoas**. This feature creates a writable signal graph which directly modifies the state in the store when you call `set` on it. Because it is a deep writable it works well with template driven forms as well as signal forms. The following code snippet shows how to set up the store with this feature:
@@ -263,7 +267,7 @@ To send changed state back to the server you can use the `withHypermediaAction`
263
267
The `_actions` key holds the metadata for the actions available for this object. In this case there is an action called `update` which uses the HTTP verb `PUT` to send the updated connection object back to the server to the specified URL in the `href` property.
264
268
265
269
:::info
266
-
If your metadata have different names or structure you can customize this via the `HateoasConfig` when you provide the **ngrx-hateoas** services in your application. For this have a look into the [Metadata Provider](./configuration/01-metadata-provider.md) section.
270
+
If your metadata has different names or a different structure you can customize this via the `HateoasConfig` when you provide the **ngrx-hateoas** services in your application. For this have a look into the [Metadata Provider](./configuration/01-metadata-provider.md) section.
267
271
:::
268
272
269
273
The following code snippet shows how to set up the store with an action that sends back the changed connection object to the server using the metadata provided in the resource:
Through the `withHypermediaAction` feature a method called `updateFlightConnection` is added to the store which can be called to send the connection object pointed to by the lambda in the second argument back to the server. The following code snippet shows how to call this method:
289
+
Through the `withHypermediaAction(<MethodName>, <LambdaToAffectedState>, <ActionName>)` feature a method called `updateFlightConnection` is added to the store which can be called to send the connection object pointed to by the lambda in the second argument back to the server. The following code snippet shows how to call this method:
Copy file name to clipboardExpand all lines: doc/docs/guide/02-concept.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ You don't need to follow the exact same layout as in the example to embed metada
86
86
87
87
## What is HATEOAS?
88
88
89
-
HATEOAS (Hypermedia as the Engine of Application State) is an architectural paradigm that allows clients to dynamically navigate resources through hyperlinks provided in responses. Possible state changes are provided via actions. Hyperlinks and actions are metadata sent next to the actual payload. Instead of hardcoding API endpoints, clients rely on these links to discover available actions and related resources. For example, if a user is not allowed to navigate to a linked resource or execute an action, the server would not send this metainformation to the client within its response. Finally, the client has the full state of the resource available, the actual payload, and information to related data and possible actions (or state changes). This approach decouples the client from the server, keeps domain logic away from the client and enables more flexible and evolvable APIs.
89
+
HATEOAS (Hypermedia as the Engine of Application State) is an architectural paradigm that allows clients to dynamically navigate resources through hyperlinks provided in responses. Possible state changes are provided via actions. Hyperlinks and actions are metadata sent next to the actual payload. Instead of hardcoding API endpoints, clients rely on these links to discover available actions and related resources. For example, if a user is not allowed to navigate to a linked resource or execute an action, the server would not send this metainformation to the client within its response. Finally, the client has the full state of the resource available, the actual payload and information to related data and possible actions (or state changes). This approach decouples the client from the server, keeps domain logic away from the client and enables more flexible and evolvable APIs.
90
90
91
91
## Hypermedia at the Backend
92
92
To create hypermedia responses you can use community libraries for the different technologies:
***resourceName**: The name of how the resource will be declared in the store.
15
+
***initialValue**: The initial value of the resource before it is loaded from a URL.
16
+
***url**: The URL from which the resource will be loaded. Can also be a function returning a string. If a function is provided, it will be executed at the time the store is created.
17
+
18
+
## State
19
+
With this feature the following state properties are added to the interface of the store:
20
+
21
+
### Resource State
22
+
```ts
23
+
<resourceName>: DeepSignal<TResource>
24
+
```
25
+
A deep signal containing the data of the resource.
26
+
27
+
### Resource Meta State
28
+
```ts
29
+
<resourceName>State: DeepSignal<
30
+
{
31
+
url: string,
32
+
isLoading: boolean,
33
+
isLoaded: boolean
34
+
}>
35
+
```
36
+
A deep signal containing meta information about the resource.
37
+
38
+
***url**: The URL from which the resource was last loaded.
39
+
***isLoading**: Whether the resource is currently being loaded.
40
+
***isLoaded**: Whether the resource currently in the state was loaded from the server.
41
+
42
+
## Methods
43
+
44
+
With this feature the following methods are added to the interface of the store:
0 commit comments