Skip to content

Instantly share code, notes, and snippets.

@arbinish
Created November 27, 2014 06:39
Show Gist options
  • Save arbinish/f181b37bef294979a5ec to your computer and use it in GitHub Desktop.
Save arbinish/f181b37bef294979a5ec to your computer and use it in GitHub Desktop.
Bootstrap typeahead example. Handling a list of objects from API endpoint.
('input[type="text"]').typeahead
minLength: 3
quietMillis: 250
source: (query, process) ->
@options.map = {}
suggest = []
self = @
$.getJSON "api/users", {q: query}, (data) =>
$.each data, (idx, item) ->
ele = "#{item.fullname} (#{item.name})"
self.options.map[ele] = item
suggest.push(ele)
process(suggest)
updater: (item) ->
return @options.map[item].name
# Sample resonpse from api/users
# [{name: "peter", fullname: "Peter Parker"}, {name: "bruce", fullname: "Bruce Wayne"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment