mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
feat: enable bind:
syntax for <Await/>
component (#1158)
This commit is contained in:
parent
b24910271a
commit
76b73acb30
1 changed files with 54 additions and 6 deletions
|
@ -10,6 +10,9 @@ use leptos_reactive::{
|
|||
/// server function directly into your view. This is the equivalent of combining a
|
||||
/// [`create_resource`] that only loads once (i.e., with a source signal `|| ()`) with
|
||||
/// a [`Suspense`] with no `fallback`.
|
||||
///
|
||||
/// Adding `bind:{variable name}` to the props makes the data available in the children
|
||||
/// that variable name, when resolved.
|
||||
/// ```
|
||||
/// # use leptos_reactive::*;
|
||||
/// # use leptos_macro::*;
|
||||
|
@ -24,11 +27,12 @@ use leptos_reactive::{
|
|||
/// view! { cx,
|
||||
/// <Await
|
||||
/// future=|cx| fetch_monkeys(3)
|
||||
/// view=|cx, data| {
|
||||
/// view! { cx, <p>{*data} " little monkeys, jumping on the bed."</p> }
|
||||
/// }
|
||||
/// />
|
||||
/// bind:data
|
||||
/// >
|
||||
/// <p>{*data} " little monkeys, jumping on the bed."</p>
|
||||
/// </Await>
|
||||
/// }
|
||||
/// # ;
|
||||
/// # });
|
||||
/// # }
|
||||
/// ```
|
||||
|
@ -43,7 +47,51 @@ pub fn Await<T, Fut, FF, VF, V>(
|
|||
blocking: bool,
|
||||
/// A function that takes a [`Scope`] and a reference to the resolved data from the `future`
|
||||
/// renders a view.
|
||||
view: VF,
|
||||
///
|
||||
/// ## Syntax
|
||||
/// This can be passed in the `view` children of the `<Await/>` by using the
|
||||
/// `bind:` syntax to specify the name for the data variable.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use leptos::*;
|
||||
/// # if false {
|
||||
/// # run_scope(create_runtime(), |cx| {
|
||||
/// # async fn fetch_monkeys(monkey: i32) -> i32 {
|
||||
/// # 3
|
||||
/// # }
|
||||
/// view! { cx,
|
||||
/// <Await
|
||||
/// future=|cx| fetch_monkeys(3)
|
||||
/// bind:data
|
||||
/// >
|
||||
/// <p>{*data} " little monkeys, jumping on the bed."</p>
|
||||
/// </Await>
|
||||
/// }
|
||||
/// # ;
|
||||
/// # })
|
||||
/// # }
|
||||
/// ```
|
||||
/// is the same as
|
||||
/// ```rust
|
||||
/// # use leptos::*;
|
||||
/// # if false {
|
||||
/// # run_scope(create_runtime(), |cx| {
|
||||
/// # async fn fetch_monkeys(monkey: i32) -> i32 {
|
||||
/// # 3
|
||||
/// # }
|
||||
/// view! { cx,
|
||||
/// <Await
|
||||
/// future=|cx| fetch_monkeys(3)
|
||||
/// children=|cx, data| view! { cx,
|
||||
/// <p>{*data} " little monkeys, jumping on the bed."</p>
|
||||
/// }
|
||||
/// />
|
||||
/// }
|
||||
/// # ;
|
||||
/// # })
|
||||
/// # }
|
||||
/// ```
|
||||
children: VF,
|
||||
) -> impl IntoView
|
||||
where
|
||||
Fut: std::future::Future<Output = T> + 'static,
|
||||
|
@ -57,7 +105,7 @@ where
|
|||
} else {
|
||||
create_resource(cx, || (), move |_| future(cx))
|
||||
};
|
||||
let view = store_value(cx, view);
|
||||
let view = store_value(cx, children);
|
||||
view! { cx,
|
||||
<Suspense fallback=|| ()>
|
||||
{move || res.with(cx, |data| view.with_value(|view| view(cx, data)))}
|
||||
|
|
Loading…
Reference in a new issue