codelv / enaml-web

Build interactive websites with enaml
https://codelv.com/projects/enaml-web/
MIT License
99 stars 17 forks source link

Launching a func when rendering component for the time and without end user action #25

Closed youpsla closed 4 years ago

youpsla commented 4 years ago

Hello, I would like to launch a a func for collecting datas from the DB and assigning to component attribute at widget initialization. Currently, I have this working by launching my function in my main.py file in the tornado RequestHandler. I would like to make my component containing the maximum code it use.

enamldef MyComponent(Html):
    attr datas

    async func get_datas_from_db(**parameters):
        return datas

    IOLoop.current().add_callback(get_datas_from_db, parameters) # Here is the syntax error

   Conditionnal:
        condition << datas is not None
       Looper:
            ...........

I haven't seen something like on_prepare which could be usefull (prepare)

Any idea ?

Regards

frmdstryr commented 4 years ago

I typically just define an async func get() and invoke that from the request handler.

youpsla commented 4 years ago

Thanks for your answer. That's what I have done so far. Just wondering if there was a solution for putting the call inside the component.

frmdstryr commented 4 years ago

You can use the activated event to schedule an async function but that will not wait for the call to complete.