marko-js-archive/marko-widgets

Support lazy widget initialization within Marko Widgets

shanimal opened this issue · 3 comments

Looking for a modular approach to loading Marko widgets from the file server.

The thought is that some portions of your app may never be needed. Things like dialogs, detail views, shopping carts. It would make sense to not include those in the initial site load.

Do you have any thoughts how this would look in practice based on your experience? Maybe some sample code to illustrate the pattern?

Also, are you using Lasso?

I'm thinking a lazy loaded widget would look exactly the same as a regular widget from the standpoint of template. If the application has a list of registered, properly name-spaced widgets, Marko would check the registry when it loads the template (i think it already does this) and if the widget doesn't exist, Marko would attempt to download it.

It might go something like:

  • check for the widget in the registry
  • if it doesn't exist check that lazy loading is enabled.
  • if the bundle is fully qualified on a x-domain, verify the bundle url with against a whitelist
  • if the bundle is relative get the default widget library endpoint
  • download and register the widget (JSONP on cross domains)
  • render the template or throw an error if it isn't found as usual

I'm assuming lasso could be used to bundle the components but it wouldn't be required.

Due to my lack of experience, the details of the implementation escapes me.

A dialog might look like this...
<alert-dialog-widget message="${data.error.message}" type="error" />

I'm assuming the widgets are properly bundled according to project needs. For instance a dialog bundle might include multiple widgets.
<alert-dialog-widget message="${data.error.message}" type="error" bundle="./dialog" />

But a widget located on a CDN might look like this
<widget-marko-cart bundle="http://marko-widget-cdn.org/components/shopping/">

Configuration could be passed of course
<widget-marko-cart bundle="${config.roots.shopping}">

One problem I've encountered in the past is optimizing so that you aren't loading code you already have. For instance we wouldn't need to include Marko and jQuery since we already have those.