1.8.0 • Published 1 year ago
@joinbox/async-loader v1.8.0
Async Loader
Component that fetches contents asynchronously and displays it when ready:
- support loading indicator
- supports error handling
Example
<async-loader
data-endpoint-url="/testContent.html"
data-trigger-event-names="loadData1,loadData2"
data-trigger-event-filter="(event.type === 'loadData1' && event.detail.loadAsync === true) || event.type === 'loadData2'"
>
<div data-content-container>Initial Content</div>
<template data-loading-template>Loading ...</template>
<template data-error-template>Error: {{message}}</template>
</async-loader>
<button class="regularContentButton">Load</button>
<script>
document.querySelector('.regularContentButton').addEventListener('click', () => {
const options = { bubbles: true, detail: { loadAsync: true } };
window.dispatchEvent(new CustomEvent('loadData', options));
});
</script>Components
Async Loader
Exposed Element
<async-loader></async-loader>
Attributes
data-endpoint-url(required ifdata-event-endpoint-property-nameis not set): URL that should be fetched. If bothdata-endpoint-urlanddata-event-endpoint-property-nameare provided,data-endpoint-urlwill be preferred.(deprecated): Name of the event that causes content to be loaded; it will be listened to ondata-trigger-event-namewindow.data-trigger-event-names(required): Comma separated names of the events which will trigger the fetching of the content; they will be listened to onwindow.data-event-endpoint-property-name(required ifdata-endpoint-urlis not set): Name of the property in theeventpayload (detailproperty of the event object) which contains the endpoint URL. Has no effect ifdata-endpoint-urlis set.data-trigger-event-filter(optional): JavaScript expression that will be evaluated against the event if provided. Only if the event matches the expression, data will be loaded; if not, the event will be ignored. Only one variable is passed (theEventthats name matchesdata-trigger-event-name); it can be accessed throughevent.data-load-once(optional): if this boolean attribute is set, content will load only once, no matter how many times a valid event fires.
Content
The following elements may or must be provided within <aync-loader>:
- Any element matching
[data-content-container](required): Content (loading, error or successfully fetched content) will be placed within this element after it has been emptied. - A
templateelement that matches[data-loading-template](optional): Its content will be displayed within[data-content-container]while data is loading. - A
templateelement that matches[data-error-template](required): Its content will be displayed within[data-content-container]if loading data fails; you may use a string{{message}}within the template'stextContentto display the error message.
Events
- Dispatches
asyncLoaderFailevent if loading content fails (bubbles). - Dispatches
asyncLoaderSuccessevent if loading content succeeds (bubbles). - Both events carry a
detailobject with propertiesurl(String, deprecated),response(instance of Response) andelement(HTMLElementthat matches the dispatchingAsyncLoader).