Bloodhound.js: Converting data returned by a remote source?

I am using Bloodhound with a remote API, and I need to convert the result obtained from the remote API.

API URL https://www.googleapis.com/books/v1/volumes?q=quilting , which returns an object with the items property, which is a list. I need to return this list to Typeahead and not to the top level object.

The Bloodhound docs say there is a transform function that should do this , but I can't get it to work.

Here is my code:

 var books = new Bloodhound({ datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); }, queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: 'https://www.googleapis.com/books/v1/volumes?q=quilting' }, transform: function(response) { console.log('transform', response); return response.items; } }); books.initialize(); // instantiate the typeahead UI $('#myTextBox').typeahead(null, { displayKey: function(suggestion) { console.log('suggestion', suggestion); return suggestion.volumeInfo.title + suggestion.volumeInfo.publishedDate; }, source: numbers.ttAdapter() }); 

And JSFIddle here: http://jsfiddle.net/2Cres/46/

This does not work because I need to pass the list of items to the typeahead user interface, but this does not seem to be happening.

+6
source share
1 answer

Try converting the translation inside the remote option, for example:

 remote { url:"fdsfds", transform: function (response){...} } 
+6
source

Source: https://habr.com/ru/post/986051/


All Articles