Created
November 4, 2018 12:39
-
-
Save o0Ignition0o/e8b467607046fe220e31e167187d98fa to your computer and use it in GitHub Desktop.
yew rust framework: mount component to dom macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub enum MountError { | |
QuerySelectorError, | |
ElementNotFound(String) | |
} | |
impl From<TODO> for MountError { | |
fn from(_: TODO) -> Self { | |
MountError::QuerySelectorError | |
} | |
} | |
macro_rules! mount { | |
($element:expr, $model:ty) => {{ | |
|()| -> Result<(),MountError> { | |
if let Some(element) = document().query_selector($element)? { | |
App::<$model>::new().mount(element); | |
Ok(()) | |
} else { | |
Err(MountError::ElementNotFound(format!("Could not find the element {} in the DOM.", $element))) | |
} | |
} | |
}}; | |
} | |
pub fn mount_app() -> () { | |
mount!("#banner", Banner); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment