3

I'm using the gems sunspot_solr, sunspot_rails and twitter-typeahead-rails as outlined in this tutorial. Everything is integrated and it's working well.

However, I notice that if the user pauses while typing, multiple sequential requests are made, using the values typed up to that point.

Eg, when searching for Elizabeth Street Sydney there are multiple GET requests:

enter image description here

The request is fired by Typeahead and uses the Bloodhound suggestion engine with the remote option pointing to a Search controller, which returns the search results in JSON format:

var addresses = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: '/search/query.json?q=%QUERY',
    wildcard: '%QUERY'
    }
  });

$('.typeahead').typeahead(null,{
  name: 'addresses',
  source: addresses
  }
}

Because these requests execute in order, the suggested text is not displayed until the last request finishes.

Is it possible to cancel a previous request when a new request is made? (Alternatively, is there a better way to avoid running multiple queries?)

Stephen Lead
  • 1,904
  • 5
  • 23
  • 46

0 Answers0