Skip to content

Python object methods get lost somewhere in the JS side #1336

Answered by hoodmane
marimeireles asked this question in Q&A
Discussion options

You must be logged in to vote

Is it possible that the problem is that you forgot to return the function from the decorator? I think a reasonable decorator should look as follows:

def when(event=None, id=None):
    def decorator(func):
        element = js.document.getElementById(id)
        sig = inspect.signature(func)

        # Function doesn't receive events
        if not sig.parameters:
            def wrapper(*args, **kwargs):
                func()
            proxy = create_proxy(wrapper)
            element.addEventListener(event, cast(wrapper, proxy))
        else:
            proxy = create_proxy(func)
            element.addEventListener(event, func)
        return func # <================================…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@marimeireles
Comment options

Comment options

You must be logged in to vote
1 reply
@marimeireles
Comment options

Answer selected by marimeireles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants