Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra data for ajax call of typeahead.js remote option

Tags:

typeahead.js

I'd like to ask how I could add extra params for the AJAX call made by remote option. I have following simple form to search for qualification (it's in Coffeescript):

$('#search_qualification').typeahead
  name:  'qualification'
  limit: 50
  remote:
    url: "/search/qualification?term=%QUERY"

I'd like to send another param with the AJAX call, that is state, normally with jQuery AJAX call, I'd do this:

$.ajax
  url: "/search/qualification"
  dataType: "json"
  data:
    term: request.term
    state: $("#state").val()

I am totally clueless in adapting this for typeahead.js, can someone please help me out here? Many thanks

like image 346
Trung Lê Avatar asked Jul 24 '13 11:07

Trung Lê


1 Answers

Use the replace option:

$('#search_qualification').typeahead({
  name:  'qualification'
  limit: 50
  remote: {
    url: '/search/qualification?term=%QUERY&state=%STATE'
    replace: function(url, query) {
      var state = encodeURIComponent($('#state').val());

      return url.replace('%QUERY', query).replace('%STATE', state);
    }
  }
});
like image 132
jharding Avatar answered Sep 27 '22 22:09

jharding