I'm having difficulty finding an autocomplete window to work with Play 2.0. I am trying to do the same as described in this article , only with Play 2.0.
Basically, I defined an action:
def autocompleteSearch(term : String) = Action { // Do something to get a list of suggestions val list = ... // Reply with list Ok(Json.toJson(list)) }
I added a route to the routes file:
GET /autocompleteSearch controllers.Application.autocompleteSearch(term: String)
I created an autocomplete script:
$(function() { $('input.autocomplete').each( function() { var $input = $(this); var serverUrl = $input.data('url'); $(this).autocomplete({ source:serverUrl }); }); });
And I insert the following on the page:
<input name="search" class="autocomplete" data-url="@{routes.Application.autocompleteSearch()}">
Unfortunately this will not work! I get the following compilation error:
not enough arguments for method autocompleteSearch: (term: String)play.api.mvc.Call. Unspecified value parameter term.
If I do this, I get the following error:
<input name="search" class="autocomplete" data-url="@{routes.Application.autocompleteSearch}"> missing arguments for method autocompleteSearch in class ReverseApplication; follow this method with `_' if you want to treat it as a partially applied function
Considering it as a partial application does not lead to the result that I want, since the resulting HTML looks like this:
<input name="search" class="autocomplete" data-url="<function1>">
In the end, I need to provide a dummy argument to get the return route:
<input name="search" class="autocomplete" data-url="@{routes.Application.autocompleteSearch( "" )}">
But then my autocomplete is useless, because it always calls a function with an empty string ...
Has anyone managed to get this to work?
Thanks in advance for your help!
==
Follow up question:
Good. So I followed the document and defined it in Application.scala:
def javascriptRoutes = Action { import routes.javascript._ Ok( Routes.javascriptRouter("jsRouter")( routes.javascript.Application.autocompleteSearch ) ).as("text/javascript") }
I added this script to the page:
<script type="text/javascript" src="@routes.Application.javascriptRoutes"></script>
Now, how do I change the input field for connecting dots?
It:
<input name="search" class="autocomplete" data-url="jsRouter.controllers.Application.autocompleteSearch">
or
<input name="search" class="autocomplete" data-url="jsRouter.controllers.Application.autocompleteSearch().ajax()">
?
Nothing works!
I have to say, I am either very stupid that I canβt understand this, or there is a big lack of documentation on this function ...