Developers

Interceptors

Interceptors are functions that modify the data before it is shown.

The interceptor function should return a resolved promise with the modified data. To set it to a component, set this component so that it overwrites its basic function to return the data without modification.

Contents interceptor

Use this interceptor to modify the contents before they are printed. The setting is setContentsDataInterceptor.

Example
To add an icon in each content:

    var results = sdk.component('results', '#results');
    results.setContentsDataInterceptor(
        function (contents){
            for (var i = 0; i < contents.length; i++) {
                contents[i]['title'] = '<span class="title__icon"> > </span>'+contents[i]['title'];
            }
            return Promise.resolve(contents);
        }
    );
Categories interceptor

Use this interceptor to modify the categories before they are printed. The setting is setCategoriesDataInterceptor.

Example
To add an icon in each category:

    var categories = sdk.component('categories', '#categories');
    categories.setCategoriesDataInterceptor(
        function (categories){
            for (var i = 0; i < categories.length; i++) {
                categories[i]['name'] = '<span class="title__icon"> > </span>'+categories[i]['name'];
            }
            return Promise.resolve(categories);
        }
    );
Typeahead autocomplete interceptor

Use this interceptor to modify the typeahead autocomplete data before it is printed. The setting is setAutocompleterDataInterceptor.

Example
To add a '>' sign in each typeahead autocomplete object:

    var autocompleter = sdk.component('autocompleter', '#autocompleter');
    autocompleter.setContentsDataInterceptor(
        function (hits){
            for (var i = 0; i < hits.length; i++) {
                hits[i]['highlighted_repr'] = "> "+hits[i]['highlighted_repr'];
            }
            return Promise.resolve(hits);
        }
    );

Use this interceptor to modify the related contents before they are printed. The setting is setRelatedDataInterceptor.

Example
Modify the related title with a custom prefix:

    var results = sdk.component('results', '#results', {
        contents: {
            showRelated: true
        }
    });
    results.setRelatedDataInterceptor(
        function(related){
            for (var i = 0; i < related.length; i++) {
                related[i]['title'] = `Related Content: ${related[i]['title']}`;
            }
            return Promise.resolve(related);
        }
    )