Containers can be reloaded when "events" occur. Triggering events is application-specific, it could come from a server action, or from JS.
Demo works by:
-
Elements (buttons, links, etc) define a
data-container-event
attribute, value is an event name. -
Elements inside a container define a
data-container-reloadon
attribute, value is an event name. -
An event listener is attached to the document that listens for
appEvent
DOM events. -
When clicking on an element defined in step 1, "app" code calls
postEvent
with the event name specified indata-container-event
. This dispatches a DOM event ofappEvent
with the passed event as a parameter. -
The listener from step 3 receives the
appEvent
, extracts the event's name, and finds all elements with[data-container-reloadon=<event name>]
. For each element, it finds the nearest ancestor "container" (element with adata-container-id
). -
The list of containers found in step 5 is passed to
reloadContainers
which requests only the specific containers from the server. -
The JSON result of 6 is used to replace (via
innerHTML
) the existing content of the contaners found in step 5 with the data from the server in step 6.