#web #wasm #javascript #router

plaster-router

A router for plaster-based frontend web applications

6 releases

0.1.5 Jul 13, 2019
0.1.4 May 27, 2019
0.1.2 Apr 13, 2019
0.1.1 Feb 5, 2019

#55 in #router

22 downloads per month
Used in plaster-router-macro

MIT/Apache

135KB
3K SLoC

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>
      }
    }
  }
}

Dependencies

~8.5MB
~172K SLoC