This HOWTO explains how to convert sync components into async components in Ractive.
- Create a simple component.
- Use it anywhere you like in your app.
- When you need to remove it from your main bundle and load asynchronously:
-
Create a synchronizer proxy with your original component's name
+ Ractive.partials.foo = getSynchronizer();
-
Add
ASYNC
postfix to your original component name- Ractive.components.foo = Ractive.extend(...) + Ractive.components.fooASYNC = Ractive.extend(...)
-
Remove
fooASYNC
(and its dependencies) from your bundle and load it any time in the future with any method you like (XHR, websockets, etc...) -
After loading
fooASYNC
in runtime, send a signal to the synchronizer and your "component"foo
will be replaced byfooASYNC
.
-
- No modifications needed in your production app.
- Convert any sync component into async component with only 2 lines of modification.
- Control when, if and how you would load the component.
https://aktos.io/st/#/showcase/async-comp
Write a simple (sync) component and use it in many places in your application:
Ractive.components.foo = Ractive.extend({
template: `<button on-click="bar" class="red {{class}}">{{yield}}</button>`
});
new Ractive({
el: 'body',
template: `
<ul>
{{#each Array(3)}}
<li>
<foo class="blue" on-bar="@global.alert('hey' + @index)">num #{{@index}}</foo>
</li>
{{/each}}
</ul>
`
})
// create foo synchronizer as a placeholder while we are loading fooASYNC component
Ractive.partials.foo = getSynchronizer();
var fooUrl = 'https://url/to/foo.ractive.js'
new Ractive({
...
oncomplete: function(){
getScript(fooUrl, () => {
this.set('@shared.deps', {_all: true});
})
}
})
You may also want to supply a "loading state" version of the async component to
provide a basic functionality to the user (eg. providing a textarea
while waiting for
ace-editor
might be a good idea):
-
Create a component as usual:
Ractive.components.fooLOADING = Ractive.extend(...)
-
Pass this component name to
getSynchronizer()
:Ractive.partials.foo = getSynchronizer('fooLOADING');
In this case, fooLOADING
component is rendered wherever foo
is used at the beginning. When fooASYNC
(the actual component) is ready, fooLOADING
is replaced by fooASYNC
.