plaster-router

Crates.ioplaster-router
lib.rsplaster-router
version0.1.5
sourcesrc
created_at2019-02-05 20:08:24.662587
updated_at2019-07-13 00:57:11.9868
descriptionA router for plaster-based frontend web applications
homepagehttps://github.com/carlosdp/plaster
repositoryhttps://github.com/carlosdp/plaster
max_upload_size
id112963
size8,498
Carlos Diaz-Padron (carlosdp)

documentation

https://docs.rs/plaster-router/

README

Usage

use plaster_router::{Routes, route_to};

#[derive(Routes)]
pub enum MyRoutes {
  #[route("/posts")]
  Posts,
  #[route("/posts/:id")]
  Post { id: String },
}

pub struct MyComponent {
  router: Router<MyRoutes>,
}

impl Component for MyComponent {
  fn create(_:_, mut context: ComponentLink<Self>) -> MyComponent {
    let mut router = MyRoutes::router(context.send_back(|| Msg::RouteUpdate));

    MyComponent {
      router: router,
    }
  }

  fn update(msg: Msg) -> ShouldRender {
    match msg {
      Msg::RouteUpdate => true,
      Msg::RouteTo(route) => { route_to(&route); true },
    }
  }
}

impl Renderable<MyComponent> on MyComponent {
  fn view(&self) -> Html<MyComponent> {
    match self.router.resolve() {
      Some(MyRoutes::Posts) => html! {
        <button onclick=|_| Msg::RouteTo("/posts/1".to_string()),>Post 1</button>
      },
      Some(MyRoutes::Post { id }) => html! {
        <h1>{format!("Post {}", id)}</h1>
      },
      None => html! {
        <h1>404 Not Found</h1>
      }
    }
  }
}
Commit count: 700

cargo fmt