pub trait Load:
Sized
+ Send
+ Sync
+ 'static {
// Required method
fn load(cx: &Cx) -> impl Future<Output = Result<Self, Error>> + Send;
}Expand description
A value a panel loads once per request, for what decides without awaiting: a
Policy, a ResourceDef::query_fn, a
condition on a column or an action. Panel::load registers it and loaded reads it.
A policy is asked once per row and once per button, so it reads what the request loaded instead of querying each time:
#[derive(Debug, toasty::Model)]
struct Subscription {
#[key]
#[auto]
id: uuid::Uuid,
team: TenantId,
projects: i64,
}
/// How many projects the request's team may hold.
struct Plan {
projects: i64,
}
impl Load for Plan {
async fn load(cx: &Cx) -> Result<Self> {
let Some(team) = tenant_id(cx) else {
return Ok(Self { projects: 0 });
};
let mut db = db(cx)?;
let subscriptions = Subscription::filter(Subscription::fields().team().eq(team))
.exec(&mut db)
.await?;
Ok(Self {
projects: subscriptions.first().map_or(1, |held| held.projects),
})
}
}
fn project_policy() -> impl Policy<Project> {
|cx: &Cx, ability: Ability<'_, Project>| match ability {
Ability::Create => loaded::<Plan>(cx).is_some_and(|plan| plan.projects > 0),
_ => true,
}
}
Panel::new("app").load::<Plan>();load runs after the auth gate admitted the request, so it reads the
signed-in user with auth::user and the tenant the request acts for with
tenant_id. On a panel whose authentication is disabled it runs on every
request. It does not run for the pages a signed-out visitor reaches, such as the login page.
It runs on every request the panel answers, the runtime requests of its pages included: keep it to what the panel’s policies read, in one or two statements.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".