0

I am trying to use the Typeahead and bloodhound framework to search a response of api paths, from a remote API call.

by example url api.example.com/getEndpoints I would receive an object containing all the endpoints. I'd want the typeahead to filter those endpoints. As far as I understand you want to specify a query when using the remote, which I obviously can't, as I just receive all the endpoints in one request.

Is there any way, or suggestion, to solve this issue?

My code would look something like

    // Instantiate the Bloodhound suggestion engine
var endpoints = new Bloodhound({
    datumTokenizer: function (datum) {
        console.log('datumTokenizer, datum: ', datum, ', datum.value: ', datum.value);
        return Bloodhound.tokenizers.whitespace(datum.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'https://api.example.com/getEndpoints',
        filter: function (endpoints) {
            console.log(endpoints);
            // Map the remote source JSON array to a JavaScript object array
            return Object.keys(endpoints).map(function (endpointPath) {
                console.log(endpointPath);
                return {
                    value: endpointPath
                };
            });
        }
    },
    limit: 10
});

// Initialize the Bloodhound suggestion engine
endpoints.initialize();

// Instantiate the Typeahead UI
$('#inputPath').typeahead(
    {
        hint: true,
        highlight: true,
        minLength: 1
    }, {
        displayKey: 'value',
        source: endpoints.ttAdapter()
    }
);
Daniel
  • 469
  • 5
  • 15

1 Answers1

0

I think you want prefetch rather than remote.

Have a look at https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#prefetch

Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • I thought so aswell - tryed it, and it wont work.... :-) Do you have any code examples of how to do it properly, perhaps? – Daniel Sep 07 '16 at 11:48