Skip to content

Instantly share code, notes, and snippets.

@Genkilabs
Created February 20, 2012 19:39
Show Gist options
  • Save Genkilabs/1870941 to your computer and use it in GitHub Desktop.
Save Genkilabs/1870941 to your computer and use it in GitHub Desktop.
Pass additional parameters into jQueryUI autocomplete widget in rails 3.1 coffeescript
#turn on our autocompletes if there are any
jQuery ->
if $('#user_autocomplete').length
index = 1
$('#user_autocomplete').autocomplete
source: (request, response) ->
#add our custom index parameter to the call
request["index"] = index++
#make our request and handle the response as normal
$.ajax(
url: "/users/autocomplete",
dataType: "json",
data: request
,
success: (data)->
response(data)
)
select: (event,ui) ->
#when the close callback is called, we know it was due to selection not focus
@selected = true
#append our custom html to the dom
$(ui.item.html)
.appendTo('#user_alerts')
.hide()
.fadeIn()
close: () ->
if @selected
#clear whatever the user entered, if this closed due to a selection
$('#user_autocomplete').val("")
@selected = false
@Genkilabs
Copy link
Author

The goal here was to have a text field with the autocomplete widget which would pass additional parameters to the server. The server code renders custom html into the response which can then be added to the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment