Ready Events
Choose ready events to initialize Web Components and requests safely.
There are up to three essential events emitted on the main document.
ffReady
This event is fired when the Web Components core library has loaded and is fully functional. The Web Components DOM elements, however, have not yet finished initializing and will likely not respond to interaction.
Subscribe to this event whenever you need to register event handlers to manipulate the request/response pipeline or to generally configure the Web Components application through JavaScript. This is likely the most frequently used of the ready-events.
All elements are utilizing the same Core functions described in ff-core.d.ts or in the Core API documentation.
The event handler receives an event object with three additional properties:
factfinder- reference to the globalfactfinderobjecteventAggregator- reference tofactfinder.communication.EventAggregatorresultDispatcher- reference tofactfinder.communication.ResultDispatcher
Example usage
<script>
document.addEventListener("ffReady", event => {
// event.factfinder === window.factfinder
// event.eventAggregator === window.factfinder.communication.EventAggregator
// event.resultDispatcher === window.factfinder.communication.ResultDispatcher
const factfinder = event.factfinder;
const eventAggregator = event.eventAggregator;
const resultDispatcher = event.resultDispatcher;
// Add event listeners and configuration as required.
});
</script>
<script defer src="pathToFFWebComponents/dist/bundle.js"></script>You can use destructuring to access these properties directly.
ffCommunicationReady
This event is emitted only when the ff-communication element is in the DOM. It fires once ff-communication finishes connecting to the DOM and propagated its configuration to the Web Components application state.
Especially, because the timing of ffReady and WebComponentsReady may vary depending on a browser's loading speed or whether the page is loaded in the background, ffCommunicationReady is the safest choice for actions directly involving the DOM elements.
ffCommunicationReady is emitted at the moment ff-communication would send the initial search request when the search-immediate attribute is set. As you typically want to set up some configuration when you subscribe to this event, it is recommended to not set the search-immediate attribute on ff-communication but instead initiate the first search request yourself.
The event object passed to the event handler has two additional properties:
factfinder- reference to the globalfactfinderobjectsearchImmediate- a function that invokes the same request as thesearch-immediateattribute would
Also see ff-communication.
WebComponentsReady
The WebComponentsReady event is fired when all FactFinder Web Components are ready to use.
All FactFinder Web Components are unknown to the browser until the JavaScript is loaded and each Web Component is registered and upgraded. This means no functions, attributes or behaviors are working until an element is upgraded.
Let's consider the following case:
This will actually result in an error.
As described above, the ffReady event indicates the core library is ready but not the elements. At the time the search is executed, the ff-communication element isn't initialized yet and therefore no communication information is available.
Instead, we want to use the WebComponentsReady to wait until all FactFinder Web Components are upgraded:
Be aware that the dispatching of FactFinder responses to all subscribers is cached and postponed until all components have been upgraded. Current added callbacks are invoked through factfinder.communication.ResultDispatcher.addCallback as soon as the FactFinder response arrives. In case any of the callbacks has side effects other than manipulating the response, this might lead to unexpected behavior.
Element Order
Even without using ffReady to trigger a search you may stumble across an error message like:
If you are sure the message is not related to custom JavaScript code, it is possibly related to the order of elements.
Here is a wrong example:
The ff-recommendation element is responsible for querying the FactFinder recommendation API. At the time the element is upgraded and sends its request, ff-communication hasn't registered all its configuration and therefore the request can't even be sent because no URL and CHANNEL information are available yet.
To fix this issue you have to place the ff-communication element before the ff-recommendation element:
Last updated
Was this helpful?