maomi
Introduction
Guide
Documents

Component Events

Using events in components

Components can trigger events to its users.

An event can contain a piece of data "T", wrapped as "Event<T>". The event data should be provided when triggering it.

#[component(Backend = DomBackend)]
struct MyChild {
template: template! {
<div> "This line is in the child component." </div>
},
pub my_event: Event<String>,
}
impl Component for MyChild {
fn new() -> Self {
Self {
template: Default::default(),
my_event: Default::default(),
}
}
fn created(&self) {
let mut data = "my event data".to_string();
self.my_event.trigger(&mut data);
}
}
#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<MyChild my_event=@handle_my_event() />
}
}
impl Component for MyWebsite {
fn new() -> Self {
Self {
template: Default::default(),
}
}
}
impl MyWebsite {
fn handle_my_event(this: ComponentEvent<Self, String>) {
log::debug!("{}", this.detail());
}
}

Note that the event fields should be properly "pub" so that it can be assigned from templates in other rust modules.