Custom Components
Build custom components with FactFinder events and result subscriptions.
FactFinder Web Components API can be used to implement custom components in any web framework.
In a typical scenario, use EventAggregator's addFFEvent() to query FactFinder and ResultDispatcher's subscribe() to receive its response.
Below are two examples implementing a product carousel component: one in VueJS and one as a native custom element.
VueJS
The source code is available here.
addFFEvent
Call addFFEvent() in the mounted hook to trigger a FactFinder search when the component renders for the first time:
eventAggregator.addFFEvent({
type: 'search',
query: carousel.query,
topics: function () {
return ['carousel-' + carousel._uid];
}
});type: 'search'indicates that we want to perform a regular search.queryis populated from the component prop.The
topicsfunction defines to which topic the results will be dispatched. In this case, this topic should be unique for each component instance. Otherwise, other carousels would interfere with each other.
subscribe
Subscribe to the same topic in the mounted hook:
carousel.subscriptionKey = resultDispatcher.subscribe('carousel-' + carousel._uid, function (response) {
carousel.records = response.searchResult.records.map(function (record) {
return record.record;
});
});Here, extract all the information needed from the response and store it in component data alongside the subscription key returned by subscribe().
Native custom element
The source code is available here.
The usage of FactFinder Web Components API here is very similar to the usage in the VueJS example.
subscribe
Subscribe in the connectedCallback:
element._subscriptionKey = resultDispatcher.subscribe('carousel-' + element._uid, handleRecords(element));
...
function handleRecords(element) {
return function(response) {
element._records = response.searchResult.records;
element._carouselContainer.innerHTML = element._records.map(renderProduct).join('');
};
}Just like in the previous example, store the subscription key and returned records.
As no automatic renderer is used, perform the rendering here.
Last updated
Was this helpful?