Skip to content

Instantly share code, notes, and snippets.

@jalevin
Created May 2, 2013 20:28
Show Gist options
  • Save jalevin/5505144 to your computer and use it in GitHub Desktop.
Save jalevin/5505144 to your computer and use it in GitHub Desktop.
window.auto_complete = (field,url, updated_field, callback) ->
labels = null
mapped = null
$(field).typeahead
source: (query, process)->
$.get(url, { q: query }, (data) ->
labels = []
mapped = {}
mapitem = (item)->
mapped[item.label] = item.value
labels.push(item.label)
mapitem(item) for item in data
process(labels)
)
updater: (item)->
id_field = $(updated_field)
id_field.val(mapped[item])
callback(id)
console.log(id_field.val())
return item
items: 5
auto_complete('#select_intake_contact_name'
'/contacts/list.json'
'#select_intake_contact_id'
(id)->
$('#select_intake_submit')
.removeAttr('disabled')
.attr('href', '/contacts/' + id + '/new_intake'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment