Skip to content

Commit ed1820d

Browse files
committed
Update README.md
1 parent f406f74 commit ed1820d

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ async fn get_index(HxBoosted(boosted): HxBoosted) -> impl IntoResponse {
118118

119119
### Example: Responders
120120

121+
We can trigger any event being listened to by the DOM using an [htmx
122+
trigger](https://htmx.org/attributes/hx-trigger/) header.
123+
121124
```rust
122125
use axum_htmx::HxResponseTrigger;
123126

@@ -130,6 +133,33 @@ async fn index() -> (&'static str, HxResponseTrigger) {
130133
}
131134
```
132135

136+
...`htmx` even allow arbitrary data to be sent along with the event, which we
137+
can use via the `serde` feature flag and the `HxEvent` type.
138+
139+
```rust
140+
use axum_htmx::HxEvent;
141+
142+
// Note that we are using `HxResponseTrigger` from the `axum_htmx::serde` module
143+
// instead of the root module.
144+
use axum_htmx::serde::HxResponseTrigger;
145+
146+
async fn index() -> (&'static str, HxResponseTrigger) {
147+
let event = HxEvent::new_with_data(
148+
"my-event",
149+
json!({"level": "info", "message": {
150+
"title": "Hello, world!",
151+
"body": "This is a test message.",
152+
}}),
153+
)
154+
.unwrap();
155+
156+
("Hello, world!", HxResponseTrigger(event))
157+
}
158+
```
159+
160+
```rust
161+
```
162+
133163
### Example: Router Guard
134164

135165
```rust

0 commit comments

Comments
 (0)