Created
February 20, 2012 19:39
-
-
Save Genkilabs/1870941 to your computer and use it in GitHub Desktop.
Pass additional parameters into jQueryUI autocomplete widget in rails 3.1 coffeescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.