ソースを参照

introduce colon ":" prefix for method name

Falko Schindler 1 年間 前
コミット
dc85026782

+ 5 - 2
nicegui/elements/json_editor.js

@@ -33,8 +33,11 @@ export default {
     },
     run_editor_method(name, ...args) {
       if (this.editor) {
-        const evaluated_args = args.map((arg) => new Function("return " + arg)());
-        return this.editor[name](...evaluated_args);
+        if (name.startsWith(":")) {
+          name = name.slice(1);
+          args = args.map((arg) => new Function("return " + arg)());
+        }
+        return this.editor[name](...args);
       }
     },
   },

+ 2 - 2
nicegui/elements/json_editor.py

@@ -53,8 +53,8 @@ class JsonEditor(Element, component='json_editor.js', exposed_libraries=['lib/va
         If the function is awaited, the result of the method call is returned.
         Otherwise, the method is executed without waiting for a response.
 
-        :param name: name of the method
-        :param args: arguments to pass to the method (must strings containing JavaScript expressions)
+        :param name: name of the method (a prefix ":" indicates that the arguments are JavaScript expressions)
+        :param args: arguments to pass to the method (Python objects or JavaScript expressions)
         :param timeout: timeout in seconds (default: 1 second)
         :param check_interval: interval in seconds to check for a response (default: 0.01 seconds)
 

+ 6 - 3
website/documentation/content/json_editor_documentation.py

@@ -26,6 +26,9 @@ def main_demo() -> None:
 @doc.demo('Run methods', '''
     You can run methods of the JSONEditor instance using the `run_editor_method` method.
     This demo shows how to expand and collapse all nodes and how to get the current data.
+
+    The colon ":" in front of the method name "expand" indicates that the value "path => true" is a JavaScript expression
+    that is evaluated on the client before it is passed to the method.
 ''')
 def methods_demo() -> None:
     json = {
@@ -38,9 +41,9 @@ def methods_demo() -> None:
     }
     editor = ui.json_editor({'content': {'json': json}})
 
-    ui.button('Expand', on_click=lambda: editor.run_editor_method('expand', 'path => true'))
-    ui.button('Collapse', on_click=lambda: editor.run_editor_method('expand', 'path => false'))
-    ui.button('Readonly', on_click=lambda: editor.run_editor_method('updateProps', r'{readOnly: true}'))
+    ui.button('Expand', on_click=lambda: editor.run_editor_method(':expand', 'path => true'))
+    ui.button('Collapse', on_click=lambda: editor.run_editor_method(':expand', 'path => false'))
+    ui.button('Readonly', on_click=lambda: editor.run_editor_method('updateProps', {'readOnly': True}))
 
     async def get_data() -> None:
         data = await editor.run_editor_method('get')