Selaa lähdekoodia

#782 support refreshable class methods

Falko Schindler 2 vuotta sitten
vanhempi
säilyke
075640f0ad
1 muutettua tiedostoa jossa 10 lisäystä ja 3 poistoa
  1. 10 3
      nicegui/functions/refreshable.py

+ 10 - 3
nicegui/functions/refreshable.py

@@ -1,5 +1,7 @@
 from typing import Callable, List
 
+from typing_extensions import Self
+
 from ..element import Element
 
 
@@ -12,15 +14,20 @@ class refreshable:
         This method will automatically delete all elements created by the function and recreate them.
         """
         self.func = func
+        self.instance = None
         self.containers: List[Element] = []
 
-    def __call__(self, *args, **kwargs) -> None:
+    def __get__(self, instance, _) -> Self:
+        self.instance = instance
+        return self
+
+    def __call__(self) -> None:
         with Element('div') as container:
-            self.func(*args, **kwargs)
+            self.func() if self.instance is None else self.func(self.instance)
         self.containers.append(container)
 
     def refresh(self) -> None:
         for container in self.containers:
             container.clear()
             with container:
-                self.func()
+                self.func() if self.instance is None else self.func(self.instance)