Skip to content

listeners disapper when cloneing Node or El #316

@MuhannadAlrusayni

Description

@MuhannadAlrusayni

I'm trying to store nodes in structs and use them later in view.

use case:

    #[derive(Clone, Debug)]
    pub enum Msg {
        Home,
        AboutUs
    }

    #[derive(Debug)]
    pub struct Flexbox {
        items: Vec<Node<Msg>>,
    }

    fn view_flexbox(flexbox: &Flexbox) -> Node<Msg> {
        div![
            flexbox.items.clone()
        ]
    }

    #[test]
    fn test_listeners() {
        let items = vec![
            button![simple_ev(Ev::Click, Msg::Home), "Home"],
            button![simple_ev(Ev::Click, Msg::AboutUs), "About us"]
        ];

        let flexbox = Flexbox {items};
        // this panic show that flexbox items have listeners
        panic!("{:#?}", flexbox);
        let node = view_flexbox(&flexbox);
        // this panic show that node doesn't children doesn't have listeners
        panic!("{:#?}", node);
    }

It seems listeners are not cloned with the node/el:

/// Allow the user to clone their Els. Note that there's no easy way to clone the
/// closures within listeners or lifestyle hooks, so we omit them.
impl<Ms> Clone for El<Ms> {
fn clone(&self) -> Self {
Self {
tag: self.tag.clone(),
attrs: self.attrs.clone(),
style: self.style.clone(),
children: self.children.clone(),
node_ws: self.node_ws.clone(),
listeners: Vec::new(),
namespace: self.namespace.clone(),
hooks: LifecycleHooks::new(),
}
}
}

Is there way to fix this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions