Skip to content
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

[User-friendliness] Event handler without the event parameter #905

Closed
antocuni opened this issue Oct 29, 2022 · 4 comments
Closed

[User-friendliness] Event handler without the event parameter #905

antocuni opened this issue Oct 29, 2022 · 4 comments

Comments

@antocuni
Copy link
Contributor

antocuni commented Oct 29, 2022

See the Tracking issue #904 for the context.

Original question on discord

The mistake it this:

async def fetch_data():
    ...
document.getElementById('btn-test').addEventListener('click', create_proxy(fetch_data))

The error is this:

Uncaught PythonError: TypeError: fetch_data() takes 0 positional arguments but 1 was given

One related issue/discussion is #835 .
This could be solved by providing, documenting and encouraging the use of a higher level API. For example:

@event('btn-test', 'click')
async def fetch_data():
    ...

In this case, the decorator would automatically call create_proxy and addEventListener, but most importantly it could also introspect the function signature and raise an user friendly error in case the actual signature doesn't match the expected one.

@Cheukting
Copy link

I like this suggestion. The original syntax is very JS-like (as it supposes to be) and for people who are not familiar with JS, this is an easy mistake to make. With the decorator, it can help to make it easier for beginners.

@tedpatrick
Copy link
Contributor

+1 The @event decorator can also detect arguments support and optionally not pass the event.

@marimeireles
Copy link
Member

This is being under dev here

@JeffersGlass
Copy link
Member

After some discussion, this morphed into the @when decorator which was added in #1428 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Next
Development

Successfully merging a pull request may close this issue.

5 participants