I'm using Bloodhound's remote
option to provide suggestions to a Typeahead autocomplete in Typeahead.js 0.10.2. Everything is working fine when I make a single ajax request using remote's url
option;
I can use the filter
option to manipulate the suggestions, and the Typeahead is populating correctly.
However, I need the Bloodhound instance to (1) make the ajax request through remote
and then (2) make various subsequent ajax requests to gather more information about the response and further adjust the values before they are shown to the user.
Remote's ajax request searches tmdb for tv shows based on what the user has typed, and my subsequent ajax requests will gather more information about each of the tv show results so I can provide better autocomplete suggestions to the user.
My filter function currently makes those subsequent ajax requests, adjusts the suggestions appropriately, and builds an array of objects.
The problem is how to have the filter function return that array to Bloodhound. As it stands, I've tried using deffereds
and setTimeout
functions (inexpertly, no doubt), but all I get for autocomplete suggestions is "undefined."
I've searched SO extensively, and I'm aware that you can't simply return the results of an ajax request because often the return will happen before the request is complete.
The usual solution I know is to feed the ajax response to a callback, but I can't see how a callback would help me return the array from the filter function as Bloodhound requires.
I'm using jQuery and Coffeescript, but below is a skeleton of my code using jQuery and Javascript. Thanks in advance for any guidance.
var tvTitles = new Bloodhound({
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: // working url that queries tmdb,
filter: function(shows) {
var requests = function() {...}; // function that builds and returns ajax requests
$.when.apply($, requests).done(function(){
// build array of objects for bloodhound
)};
// how to make filter return the array to bloodhound?
}
}
});
// initialize tvTitles
// call typeahead with tvTitles.ttAdapter() as source