-
-
Notifications
You must be signed in to change notification settings - Fork 489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a pure JavaScript event handler #2536
Conversation
This PR still has some issues:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this pull request, @WSH032!
Regarding the two issues you mentioned:
- I merged both types of listeners, which simplifies the code and preserves their order.
eval
seems to be much easier to use thannew Function
, especially when we want to support event arguments like(e) => alert(e)
. So I think it is reasonable to keepeval
. In the end it's not arbitrary code that is executed, but code defined in a Python file on the server.
We could add another demo to the documentation, showcasing the new js_handler
. I'm just not sure about a good place for it.
I think we can provide a basic introduction to it in generic_events, informing users that we use Additionally, some Python event listeners using the https://nicegui.io/documentation/run_javascript#run_javascript My original purpose of using this feature was to design a form that requires calling some browser APIs, such as from nicegui import ui
js_handler = """\
(evt) => {
evt.preventDefault();
localStorage.setItem('password', evt.target[0].value);
alert(localStorage.getItem('password'));
}
"""
with ui.element("q-form").on("submit", js_handler=js_handler) as form:
ui.input("Password")
ui.button("Submit").props("type=submit")
ui.run(reload=True) |
I just thought of a use case where it seems we can't access the component's this. Is there any solution? from nicegui import ui
js_handler = """\
(
function(event) {
// we expect `this` is the component,
// but here it's `undefined`.
console.log(this === undefined)
}
)
"""
ui.button('button').on("click", js_handler=js_handler)
ui.run(reload=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a short example to the docs. And I wonder if we could make #2377 (comment) work. Could we somehow not go through Python at all? That would enable Safari to allow something like
ui.icon('link').on('click', js_handler='navigator.clipboard.writeText("from nicegui")')
I'm not sure if I understand your meaning. At least for the current implementation, js event handler should only run on the frontend and do not require communication with the server. In my testing, even when I completely shut down Python, the clipboard still works fine. Note, we should provide a function as the code - ui.icon('link').on('click', js_handler='navigator.clipboard.writeText("from nicegui")')
+ ui.icon('link').on('click', js_handler='() => {navigator.clipboard.writeText("from nicegui")}') |
I just added a demo to the "Generic Events" section, showing how Furthermore I made use of this new parameter to simplify the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful!
Discuss: #2383 (reply in thread)