-
Given the following app: @app("/")
async def serve(q: Q):
print("args", q.args)
if not q.client.initialized:
q.page['meta'] = ui.meta_card(box='')
q.client.initialized = True
q.client.textbox1 = q.client.textbox2 = ""
await q.page.save()
await handle_on(q)
q.page['main'] = ui.form_card(
box='4 2 3 3',
title='',
items=[
ui.textbox(name="textbox1", label="textbox 1", trigger=True, value=q.client.textbox1 or ""),
ui.textbox(name="textbox2", label="textbox 2", trigger=True, value=q.client.textbox2 or ""),
ui.text(content=q.client.changed_text or ""),
],
)
await q.page.save()
# @on("textbox1") # won't trigger when textbox is cleared, because "" is not truthy
@on("textbox1", lambda x: x is not None)
async def ontextbox1(q: Q):
print("textbox1 changed")
if q.client.textbox1 != q.args.textbox1:
q.client.changed_text = f"textbox1 value changed from {q.client.textbox1} to {q.args.textbox1}"
q.client.textbox1 = q.args.textbox1
# without the following 'ontextbox2' would never be called
q.args.textbox1 = None
await handle_on(q)
@on("textbox2", lambda x: x is not None)
async def ontextbox2(q: Q):
print("textbox2 changed")
if q.client.textbox2 != q.args.textbox2:
q.client.changed_text = f"textbox2 value changed from {q.client.textbox2} to {q.args.textbox2}"
q.client.textbox2 = q.args.textbox2
q.args.textbox2 = None
await handle_on(q)
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
The The I'm curious why you'd want to know which textbox triggered it. I can suggest alternatives if I can understand your situation better. |
Beta Was this translation helpful? Give feedback.
-
related: #952 |
Beta Was this translation helpful? Give feedback.
The
on/handle_on
duo is merely a syntactic sugar for multipleif/elif.../else
that one would normally do insideserve()
, so the behavior wherehandle_on()
calls exactly one handler is intentional. Explained here: https://wave.h2o.ai/docs/routing#reducing-boilerplateThe
trigger
simply means "submit everything when this field changes", and not "submit this field when it changes". Wave apps generally handle all available fields in aggregate, and not individually as in a traditional event-handling / callback style programming.I'm curious why you'd want to know which textbox triggered it. I can suggest alternatives if I can understand your situation better.