Commit e6c152a
Rerender
Changed files (3)
Diff
@@ -5,9 +5,8 @@ pub struct Counter { value: i32, } -impl Counter {+impl Component for Counter { fn connected(&mut self, element: HtmlElement) {- log!("Element connected"); self.render(&element); } @@ -26,8 +25,6 @@ impl Counter { old_value: &str, new_value: &str, ) {- log!("Attribute changed", name, new_value);- match name { "count" => { self.value = new_value.parse::<i32>().expect("Not an integer.");@@ -39,9 +36,7 @@ impl Counter { self.render(&element); }-} -impl Counter { fn render(&self, element: &HtmlElement) { let count = element.get_attribute("count"); @@ -64,7 +59,6 @@ impl Counter { #[callback(increment)] pub fn increment(id: String) {- log!("increment"); let html_element = get_component_element(id.as_str()); if html_element.is_none() {@@ -82,7 +76,7 @@ pub fn increment(id: String) { set_value!(&id, Counter.value, new_count); element.set_attribute("count", new_count.to_string().as_str());- rerender(&id);+ rerender::<Counter>(&id); } fn increment_component(html_element: &HtmlElement) -> i32 {@@ -103,18 +97,3 @@ fn increment_component(html_element: &HtmlElement) -> i32 { let new_count = count_res.unwrap() + 1; 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);-}@@ -324,23 +324,24 @@ pub fn get_callback(name: &str) -> Option<js_sys::Function> { /// HTML Component. See kaja-web-component. pub trait Component {- fn connected(&mut self, parent: HtmlElement) {}- fn disconnected(&mut self, parent: HtmlElement) {}+ fn connected(&mut self, _parent: HtmlElement) {}+ fn disconnected(&mut self, _parent: HtmlElement) {} fn observed_attributes() -> &'static [&'static str] { &[] } fn attribute_changed( &mut self,- parent: HtmlElement,+ _parent: HtmlElement, name: &str,- old_value: &str,- new_value: &str,+ _old_value: &str,+ _new_value: &str, ) { log!("Attribute changed but no update handler for ", name); } - fn get_component_id(&self) -> String;+ /// Render the component into the given element.+ fn render(&self, _element: &HtmlElement) {} } pub fn get_component_element(id: &str) -> Option<HtmlElement> {@@ -387,41 +388,6 @@ pub fn get_component_id(element: &HtmlElement) -> String { id.unwrap() } -pub fn update_component<F, T, S>(id: S, callback: F)-where- S: AsRef<str>,- F: Fn(&mut T, &HtmlElement),- T: ComponentStorage + 'static,-{- let id_ref = id.as_ref();- let element_opt = get_component_element(id_ref);-- if element_opt.is_none() {- error!("Element not found for id", id_ref);- return;- }-- let element = element_opt.unwrap();-- let component_opt = get_component::<T>(id_ref);-- if component_opt.is_none() {- error!("Component not found for id", id_ref);- return;- }-- let component_arc = component_opt.unwrap();- match component_arc.try_lock() {- Ok(mut component) => {- callback(&mut component, &element);- }- Err(_) => {- error!("Can't lock component for id", id_ref);- return;- }- }-}- // Example: let count = get_value!(&id, Counter.field); #[macro_export] macro_rules! get_value {@@ -441,3 +407,21 @@ macro_rules! set_value { guard.$field = $value; }}; }++pub fn rerender<T>(id: &str)+where+ T: ComponentStorage + Component + 'static,+{+ let component = get_component::<T>(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);+}@@ -1,8 +1,8 @@ pub use crate::{ CallbackRegistration, Component, ComponentStorage, Error, InitComponentFn, InitFn, Result, 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,+ get_element, get_elements, get_value, init_callbacks, inner_html, inner_text, rerender,+ set_value, try_get_value, try_inner_html, try_inner_text, }; pub use gloo::console::error;