Update docs to specify response parts ordering (fixes #11)

This commit is contained in:
Rob Wagner 2023-12-14 20:44:50 -05:00
parent 1edbce34be
commit e8a0b94d03
No known key found for this signature in database
GPG key ID: 53CCB4497B15CF61
2 changed files with 17 additions and 4 deletions

View file

@ -130,10 +130,12 @@ trigger](https://htmx.org/attributes/hx-trigger/) header.
use axum_htmx::HxResponseTrigger; use axum_htmx::HxResponseTrigger;
// When we load our page, we will trigger any event listeners for "my-event. // When we load our page, we will trigger any event listeners for "my-event.
async fn index() -> (&'static str, HxResponseTrigger) { async fn index() -> (HxResponseTrigger, &'static str) {
// Note: As HxResponseTrigger only implements `IntoResponseParts`, we must
// return our trigger first here.
( (
"Hello, world!",
HxResponseTrigger::normal(["my-event", "second-event"]), HxResponseTrigger::normal(["my-event", "second-event"]),
"Hello, world!",
) )
} }
``` ```
@ -148,7 +150,7 @@ use serde_json::json;
// instead of the root module. // instead of the root module.
use axum_htmx::{HxEvent, HxResponseTrigger}; use axum_htmx::{HxEvent, HxResponseTrigger};
async fn index() -> (&'static str, HxResponseTrigger) { async fn index() -> (HxResponseTrigger, &'static str) {
let event = HxEvent::new_with_data( let event = HxEvent::new_with_data(
"my-event", "my-event",
// May be any object that implements `serde::Serialize` // May be any object that implements `serde::Serialize`
@ -159,7 +161,9 @@ async fn index() -> (&'static str, HxResponseTrigger) {
) )
.unwrap(); .unwrap();
("Hello, world!", HxResponseTrigger::normal([event])) // Note: As HxResponseTrigger only implements `IntoResponseParts`, we must
// return our trigger first here.
(HxResponseTrigger::normal([event]), "Hello, world!")
} }
``` ```

View file

@ -111,6 +111,9 @@ pub enum TriggerMode {
/// visible ASCII (32-127) when serializing to JSON. /// visible ASCII (32-127) when serializing to JSON.
/// ///
/// See <https://htmx.org/headers/hx-trigger/> for more information. /// See <https://htmx.org/headers/hx-trigger/> for more information.
///
/// Note: An `HxResponseTrigger` implements `IntoResponseParts` and should be
/// used before any other response object would consume the response parts.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct HxResponseTrigger { pub struct HxResponseTrigger {
pub mode: TriggerMode, pub mode: TriggerMode,
@ -129,18 +132,24 @@ impl HxResponseTrigger {
/// Creates new [normal](https://htmx.org/headers/hx-trigger/) trigger from /// Creates new [normal](https://htmx.org/headers/hx-trigger/) trigger from
/// events. /// events.
///
/// See `HxResponseTrigger` for more information.
pub fn normal<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self { pub fn normal<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self {
Self::new(TriggerMode::Normal, events) Self::new(TriggerMode::Normal, events)
} }
/// Creates new [after settle](https://htmx.org/headers/hx-trigger/) trigger /// Creates new [after settle](https://htmx.org/headers/hx-trigger/) trigger
/// from events. /// from events.
///
/// See `HxResponseTrigger` for more information.
pub fn after_settle<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self { pub fn after_settle<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self {
Self::new(TriggerMode::AfterSettle, events) Self::new(TriggerMode::AfterSettle, events)
} }
/// Creates new [after swap](https://htmx.org/headers/hx-trigger/) trigger /// Creates new [after swap](https://htmx.org/headers/hx-trigger/) trigger
/// from events. /// from events.
///
/// See `HxResponseTrigger` for more information.
pub fn after_swap<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self { pub fn after_swap<T: Into<HxEvent>>(events: impl IntoIterator<Item = T>) -> Self {
Self::new(TriggerMode::AfterSwap, events) Self::new(TriggerMode::AfterSwap, events)
} }