Bloodhound.js: Transform the data returned by a remote source?
Asked Answered
F

1

8

I am using Bloodhound with a remote API and I need to transform the result returned from the remote API.

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

The Bloodhound docs say that there is a transform function that is supposed to 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 a JSFIddle here: http://jsfiddle.net/2Cres/46/

This does not work, because I need to feed the items list into the typeahead UI, but this doesn't seem to be happening.

Fear answered 27/4, 2015 at 11:55 Comment(0)
U
9

Try moving transform inside the remote option, like this:

remote {
  url:"fdsfds",
  transform: function (response){...}
}
Underdone answered 27/4, 2015 at 12:14 Comment(2)
@Kristoffer Sall-Storgaard, actually it is an answer, but, may be not in clear form, sorry for thatUnderdone
I edited your answer, if it isn't an improvement feel free to roll it back.Cummine

© 2022 - 2024 — McMap. All rights reserved.