Skip to content

Providing a closure as member of a parameter object to pass to an JS function #3885

Discussion options

You must be logged in to vote

I think something like the following will probably work

/// [Modal] configuration options.
#[wasm_bindgen]
pub struct ModalConfig {
    /// Title of the modal.
    pub title: String,
    handler: Closure<dyn Fn(bool) -> JsValue>,
}

impl ModalConfig {
    /// New modal configuration.
    pub fn new<H: Fn(bool) + 'static>(title: &str, handler: H) -> Self {
        let handler = Closure::new(handler);
        Self {
            title: title.to_string(),
	    handler
        }
    }
}

#[wasm_bindgen]
extern "C" {
    /// A modal.
    pub type Modal;
    
    /// Internal function to create the modal on JavaScript side.
    #[wasm_bindgen(js_namespace=["$"], js_name="modal")]
    fn new_moda…

Replies: 3 comments 3 replies

You must be logged in to vote
1 reply
@emirror-de
Comment options

You must be logged in to vote
1 reply
@emirror-de
Comment options

Comment options

You must be logged in to vote
1 reply
@emirror-de
Comment options

Answer selected by emirror-de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #3883 on March 12, 2024 08:14.