Module maomi::event

source ·
Expand description

The event handling utilities.

The event fields in components can be binded by component users, and triggered by the component itself.

The following example shows the basic usage of events.

use maomi::prelude::*;
 
#[component]
struct MyComponent {
    template: template! {
        /* ... */
    },
    // define an event with the detailed type
    my_event: Event<usize>,
}
 
impl Component for MyComponent {
    fn new() -> Self {
        Self {
            template: Default::default(),
            my_event: Event::new(),
        }
    }
 
    fn created(&self) {
        // trigger the event
        self.my_event.trigger(&mut 123);
    }
}
 
#[component]
struct MyComponentUser {
    template: template! {
        // set the event listener
        <MyComponent my_event=@my_ev() />
        // extra arguments can be added in the listener
        // (arguments should implement `Clone` or `ToOwned`)
        <MyComponent my_event=@my_ev_with_data("abc") />
    },
}
 
impl MyComponentUser {
    // the event listener has two preset arguments: `this` and the event detailed type
    fn my_ev(this: ComponentRc<Self>, detail: &mut usize) {
        assert_eq!(*detail, 123);
    }
 
    // with extra arguments
    fn my_ev_with_data(this: ComponentRc<Self>, detail: &mut usize, data: &str) {
        assert_eq!(*detail, 123);
        assert_eq!(data, "abc");
    }
}

Structs

  • An event that can be binded and triggered.

Traits