marko-js-archive / marko-widgets

[LEGACY] Module to support binding of behavior to rendered UI components rendered on the server or client
http://v3.markojs.com/docs/marko-widgets/
MIT License
141 stars 40 forks source link

Async fragments and widget communication #3

Closed aselvaraj closed 9 years ago

aselvaraj commented 9 years ago

The widget order is messed up when child widgets are loaded as async fragments. In other words, the parent widget is getting initialized first before the child widgets.

How to maintain the widget initialization order with async fragments?

You can reproduce the issue using this forked repo: https://github.com/aselvaraj/raptor-samples/tree/master/widgets-communication

patrick-steele-idem commented 9 years ago

Thanks for reporting the issue. I'll take a look at your forked repo and get back to you soon.

patrick-steele-idem commented 9 years ago

I investigated the issue and found the issue you are seeing is due to a limitation with how marko-widgets is implemented, but at the same time I don't think this is a limitation that we want to "fix". Each <async-fragment> should be thought of as an independent fragment of the page that could be initialized independently (especially given that they could be sent to the browser out-of-order if that option is enabled).

Internally we maintain a stack of widgets, but if nested widgets render asynchronously they lose the scope of their parent since the parent widget is popped off the stack before the async fragment renders. We could, in theory, save off the widget stack when beginning an asynchronous fragment but that will add a lot of additional complexity. Plus, that solution still would break if out-of-order flushing were used.

You might want to consider using pub/sub for the outer widget to be notified when asynchronous widgets are ready. That is, a widget nested in an <async-fragment> would need to publish a message to let the outer widget know that is ready and the outer widget could response accordingly.

Thanks for reporting the issue. If you are still stuck please let me know and we can discuss your use case some more to find a reasonable workaround.

aselvaraj commented 9 years ago

I agree with pub/sub approach and in fact I am doing that now. But, I felt the in order async fragments could benefit from the marko widget communication pattern if the order is maintained.