Supporting web_sys::Event.
[Kagura] Kagura + Rust でWebページを作成
extern crate kagura;
extern crate wasm_bindgen;
use kagura::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn main() {
kagura::run(Component::new(State, update, render), "app");
}
struct State;
struct Msg;
struct Sub;
fn update(_: &mut State, _: Msg) -> Cmd<Msg, Sub> {Cmd::none()}
fn render(_: &State) -> Html<Msg> {
Html::h1(
Attributes::new(),
Events::new(),
vec![
Html::text("hello kagura"),
],
)
}
kagura::Component::new(initial_state, update, render)
update
and render
is function :
update : fn(&mut State, Msg) -> Cmd<Msg, Sub>
render : fn(&State) -> Html<Msg>
kagura::run(component, id_of_entry_point_in_html)
kagura::Html::html_tag(attributes, events, children)
attributes
: instance of kagura::Attributes
events
: instance of kagura::Events
children
: Vec<Html>
<ul class="list example" id="my-list" data-fizz="bazz">
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
is made by
use kagura::Html;
use kagura::Attributes;
use kagura::Events;
Html::ul(
Attributes::new()
.class("list")
.class("example")
.id("my-list")
.string("data-fizz", "bazz"),
Events::new(),
vec![
Html::li(Attributes::new(), Events::new(), vec![Html::unsafe_text("foo")]),
Html::li(Attributes::new(), Events::new(), vec![Html::unsafe_text("bar")]),
Html::li(Attributes::new(), Events::new(), vec![Html::unsafe_text("baz")])
]
)
kagura::Html::component(component)
component
: instance of kagura::Component
update
can send message to parent compoent as Some(message).
component.subscribe(impl: Sub -> Box<Any>)
can receive message from child component and bind to own message.
fn render() -> Html<Msg> {
Html::component(
child_component::new().subscribe(|sub| match sub {
child_component::Sub::Foo => Msg::Bar
})
)
}
mod child_component {
fn new() -> Component<Msg, State, Sub> {
Component::new(initial_state, update, render)
}
.
.
.
}
Cmd::none()
means nothing to do. If you return Cmd::none(), kagura will render.
If you send sub-message to parent component, use this.
You can use this feature like callback function in JavaScript. like this:
fn update(state: &mut State, msg: Msg) -> kagura::Cmd<Msg, Sub> {
use kagura::Cmd;
match msg {
Msg::ChangeMessage(message) => {
state.message = message;
Cmd::none()
}
Msg::ChangeMessageTask(message) => Cmd::task(|resolver| {
let resolver = Closure::once(|| resolver(Msg::ChangeMessage(message)));
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(
resolver.as_ref().unchecked_ref(),
1000,
);
resolver.forget();
}),
}
}