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.rs
...
#[async_trait]
impl Route for CustomersRoute {
...
async fn model(&self, _params: &[Value]) -> Result<RouteModel> {
Ok(RouteModel::Json(json!({
"name": "Acme Widgets",
"totalMonthlySpend": 2300,
})))
}
}
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.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.rs
#[async_trait]
impl Route for CustomersRoute {
...
async fn model(&self, _params: &[Value]) -> Result<RouteModel> {
let customers: Vec<Customer> = self.store.find_all("customer", None).await?;
...
}
}