Commit d9014e7
Get and set macro for component
Changed files (4)
modifiedexamples/counter-component/src/counter.rsmodifiedexamples/counter-component/src/main_component.rsmodifiedsrc/lib.rsmodifiedsrc/prelude.rs
Diff
@@ -50,7 +50,7 @@ impl Counter { return; } - let component_id = self.get_component_id();+ let component_id = get_component_id(element); let content = html! {{ <p> Count: $(count.unwrap())@@ -65,13 +65,6 @@ impl Counter { #[callback(increment)] pub fn increment(id: String) { log!("increment");- let mut new_count = -1;-- update_component(&id, |component: &mut Counter, element: &HtmlElement| {- log!("update_component", component.get_component_id());- increment_component(component, element);- });- let html_element = get_component_element(id.as_str()); if html_element.is_none() {@@ -79,51 +72,49 @@ pub fn increment(id: String) { return; } - let count = get_value(&id);- let element = html_element.unwrap();- element.set_attribute("count", count.to_string().as_str());-- rerender(&id);-}+ let mut new_count = increment_component(&element); -fn rerender(id: &str) {- let component = get_component::<Counter>(id);- let binding = component.unwrap();- let component_binding = binding.lock().unwrap();- let html_element = get_component_element(id);-- if html_element.is_none() {- error!("No html_element in rerender.");+ if new_count == -1 {+ error!("Increment failed."); return; } - let element = html_element.unwrap();- component_binding.render(&element);-}--fn get_value(id: &str) -> i32 {- let component = get_component::<Counter>(id);- let binding = component.unwrap();- let c = binding.lock().unwrap();- c.value+ set_value!(&id, Counter.value, new_count);+ element.set_attribute("count", new_count.to_string().as_str());+ rerender(&id); } -fn increment_component(component: &mut Counter, element: &HtmlElement) {- let count_attribute = element.get_attribute("count");+fn increment_component(html_element: &HtmlElement) -> i32 {+ let count_attribute = html_element.get_attribute("count"); if count_attribute.is_none() { error!("Attribute 'count' not found for element");- return;+ return -1; } let count_res = count_attribute.unwrap().parse::<i32>();+ if count_res.is_err() { error!("Count is not an integer.");- return;+ return -1; } let new_count = count_res.unwrap() + 1;+ new_count+} - component.value = new_count;+fn rerender(id: &str) {+ let component = get_component::<Counter>(id);+ let binding = component.unwrap();+ let component_binding = binding.lock().unwrap();+ let html_element = get_component_element(id);++ if html_element.is_none() {+ error!("No html_element in rerender.");+ return;+ }++ let element = html_element.unwrap();+ component_binding.render(&element); }@@ -21,3 +21,20 @@ impl Main { element.set_inner_html(content.as_str()); } }++impl Main {+ fn disconnected(&mut self, _element: HtmlElement) {}++ fn observed_attributes() -> &'static [&'static str] {+ &[]+ }++ fn attribute_changed(+ &mut self,+ _parent: HtmlElement,+ _name: &str,+ _old_value: &str,+ _new_value: &str,+ ) {+ }+}@@ -376,6 +376,17 @@ pub fn get_component<T: ComponentStorage + 'static>( map.get(id).cloned() } +pub fn get_component_id(element: &HtmlElement) -> String {+ let id = element.get_attribute("componentid");++ if id.is_none() {+ error!("No component id is set for element.");+ return "".to_string();+ }++ id.unwrap()+}+ pub fn update_component<F, T, S>(id: S, callback: F) where S: AsRef<str>,@@ -410,3 +421,23 @@ where } } }++// Example: let count = get_value!(&id, Counter.field);+#[macro_export]+macro_rules! get_value {+ ($id:expr, $component:ident $(:: $comp_rest:ident)* . $field:ident) => {{+ let component = $crate::get_component::<$component $(:: $comp_rest)*>($id).unwrap();+ let guard = component.lock().unwrap();+ guard.$field.clone()+ }};+}++// Example: let count = set_value!(&id, Counter.field, value);+#[macro_export]+macro_rules! set_value {+ ($id:expr, $component:ident $(:: $comp_rest:ident)* . $field:ident, $value:expr) => {{+ let component = $crate::get_component::<$component $(:: $comp_rest)*>($id).unwrap();+ let mut guard = component.lock().unwrap();+ guard.$field = $value;+ }};+}@@ -1,8 +1,8 @@ pub use crate::{ CallbackRegistration, Component, ComponentStorage, Error, InitComponentFn, InitFn, Result,- get_callback, get_component, get_component_element, get_cookie_value, get_element,- get_elements, get_value, init_callbacks, inner_html, inner_text, try_get_value, try_inner_html,- try_inner_text, update_component,+ get_callback, get_component, get_component_element, get_component_id, get_cookie_value,+ get_element, get_elements, get_value, init_callbacks, inner_html, inner_text, set_value,+ try_get_value, try_inner_html, try_inner_text, update_component, }; pub use gloo::console::error;@@ -10,7 +10,7 @@ pub use gloo::console::log; pub use serde_wasm_bindgen; pub use std::collections::HashMap;-pub use std::sync::RwLock;+pub use std::sync::{MutexGuard, RwLock}; pub use wasm_bindgen::JsCast; pub use wasm_bindgen::JsValue;