-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
lib.rs
67 lines (59 loc) · 1.64 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
extern crate stdweb;
#[macro_use]
extern crate yew;
use stdweb::web::Date;
use yew::prelude::*;
use yew::services::console::ConsoleService;
pub struct Model {
value: i64,
}
pub enum Msg {
Increment,
Decrement,
Bulk(Vec<Msg>),
}
impl<CTX> Component<CTX> for Model
where
CTX: AsMut<ConsoleService>,
{
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, _: &mut Env<CTX, Self>) -> Self {
Model { value: 0 }
}
fn update(&mut self, msg: Self::Message, env: &mut Env<CTX, Self>) -> ShouldRender {
match msg {
Msg::Increment => {
self.value = self.value + 1;
env.as_mut().log("plus one");
}
Msg::Decrement => {
self.value = self.value - 1;
env.as_mut().log("minus one");
}
Msg::Bulk(list) => for msg in list {
self.update(msg, env);
env.as_mut().log("Bulk action");
},
}
true
}
}
impl<CTX> Renderable<CTX, Model> for Model
where
CTX: AsMut<ConsoleService> + 'static,
{
fn view(&self) -> Html<CTX, Self> {
html! {
<div>
<nav class="menu",>
<button onclick=|_| Msg::Increment,>{ "Increment" }</button>
<button onclick=|_| Msg::Decrement,>{ "Decrement" }</button>
<button onclick=|_| Msg::Bulk(vec![Msg::Increment, Msg::Increment]),>{ "Increment Twice" }</button>
</nav>
<p>{ self.value }</p>
<p>{ Date::new().to_string() }</p>
</div>
}
}
}