For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

1

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.

  • query is populated from the component prop.

  • The topics function 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.

When designing a production-ready component, it is advisable to implement a query prop watcher to react to its possible changes with consecutive search requests.

2

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().

3

unsubscribe

Before destroying the component, unsubscribe using the previously stored subscription key:

if (carousel.subscriptionKey) {
    resultDispatcher.unsubscribe('carousel-' + carousel._uid, carousel.subscriptionKey);
}

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.

1

addFFEvent

The addFFEvent() call is almost identical:

eventAggregator.addFFEvent({
    type: 'search',
    query: query,
    topics: function () {
        return ['carousel-' + uid];
    }
});

Here, invoke it in the attributeChangedCallback, meaning whenever a new value of the query attribute is set, trigger a new search.

2

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.

3

unsubscribe

Unsubscribe in the disconnectedCallback:

resultDispatcher.unsubscribe('carousel-' + element._uid, element._subscriptionKey);

Last updated

Was this helpful?