Specifying a Route's Model

You define a route's model by implementing its model() function.

Static Models

Let's return a simple model:

// src/routes/customers_route.rs
...

#[async_trait]
impl Route for CustomersRoute {
    ...
    async fn model(&self, _params: RouteParams) -> Result<RouteModel> {
        Ok(json!({
            "name": "Acme Widgets",
            "totalMonthlySpend": 2300,
        }).into())
    }
}

You can now reference the model values from your route template:

{{!-- src/templates/customers.hbs --}}

<li>Name: {{this.model.name}}</li>
<li>Total Monthly Spend: {{this.model.totalMonthlySpend}}</li>

Fetching Data

To fetch data from your model() function, first customize your route initialization to save a reference to the application-wide Store:

// src/routes/customers_route.rs

#[derive(AuricRoute, Default)]
#[route("/customers")]
pub struct CustomersRoute {
    store: Arc<Store>,
}

#[async_trait]
impl Route for CustomersRoute {
    fn init(&mut self, context: &dyn Context) -> Result<()> {
        self.store = context.store();
        Ok(())
    }
}

Now you can use the store from your model() function:

// src/routes/customers_route.rs

use crate::generated::Bucket;

#[async_trait]
impl Route for CustomersRoute {
    ...
    async fn model(&self, _params: RouteParams) -> Result<RouteModel> {
        let customers: Vec<Customer> = self.store.find_all(Bucket::Customers, None).await?;
        Ok(RouteModel::from(customers))
    }
}

See Routing / Defining Routes / Dynamic Segments for an example model() function that returns a single record using an id URL parameter.


©2026 Megalithic LLC | Website | Blog | GitLab | Contact