correct way to jump out event handler #1404
-
say, I have a textbox control, and set trigger = True, and contains default value. In such case, when user triggered any events on same page, q.args will always contains this textbox's value and trigger my handler. Since the page doesn't have to be re-rendered (at lest this handler should NOT respond this time), how to jump out the handler without render page? If I just jump out with return, seems I'll got a spin cursor in the browser. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
Hi @zillionare could you please provide a minimal reproducible example? Thank you! |
Beta Was this translation helpful? Give feedback.
-
Thanks for help me out @mtanco
when user press << button, a 'fastbackward' event is triggered, and the q.args is as following:
Unfortunelately, both I can test if |
Beta Was this translation helpful? Give feedback.
-
or, since the value |
Beta Was this translation helpful? Give feedback.
-
my suggestion is to rewrite the following code:
to pass result(True/False) of user's event handler to handle_on, if it's False, means handle_on should search other event handler, else return immediatly. In this way, I can bypass |
Beta Was this translation helpful? Give feedback.
-
I've created a PR #1414, trying to fix this. |
Beta Was this translation helpful? Give feedback.
-
@zillionare The arg handling works as designed (so we'll probably not merge your PR). The arg handling checks for presence of args, and calls the corresponding handler. In your particular case, A better way to handle this is to store the last known values of the start and end textboxes in e.g.
|
Beta Was this translation helpful? Give feedback.
-
I've already do the check, and if the value doesn't change, abort update the page. and yes, I do set
the issue is:
handler_on is a func defined in routing.py, which will go through all registered event handler, and invoke them one by one if match some criteria. The issue in my scenario is, when user clicked on button No matter what kind of result The capability to let event handler to decide whether to stop event propagation or not could be a common requirement and it's part of chain of responsibility pattern. And the capability for textbox to handle |
Beta Was this translation helpful? Give feedback.
-
The I would recommend not using Instead, simply process e.g. async def handle_toolbar(q: Q) -> bool:
# process q.args
return False # or True if handled.
@app("/", on_startup=on_startup, on_shutdown=on_shutdown)
async def serve(q: Q):
if not q.client.initialized:
await on_client_connected(q)
q.client.initialized = True
if await handle_toolbar(q):
return
# Handle routing.
await handle_on(q) If you don't want to do it this way, it's fairly simple to write and use your own |
Beta Was this translation helpful? Give feedback.
-
I've copied routing.py from h2o wave, and add StopPropagation exception handling in handle_on like this:
So if q.args can trigger multiple handler, by default all event handler will be invoked. If by change one of them want to stop the propagation, then it can raise StopPropagation so the rest handler will not be invoked. I agree h2o wave's focus at this part is to handling form/menu click well, and my scenario may not be common or, not a designed one. Wave is very convenient and promising, I think more and more python developers will find it and embrace it. So please don't lock (sorry I'm not native speaker) its power by disallow user doing common scenarios that b/s arch can do. As javascript developer can do both client/server side job, I guess python developer may feel same, at least I am. @lo5 @mtanco |
Beta Was this translation helpful? Give feedback.
The
@on
annotation is not modeled after the CoR pattern, since the common usage in Wave is form/menu click handling from different cards. CoR is more suited for event bubbling (in which case your PR for bubble cancellation makes sense).I would recommend not using
@on
/handle_on
in your case (it's an opt-in feature any way, not the only way to do things, and not suited for your scenario).Instead, simply process
q.args
inserve()
(or a function called fromserve()
), in which case you'll have access to the state of your enti…