AutoFill 2.0 playback reverse routing not working

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() { /* * Autocomplete for populating an input field with a value from the server. */ $('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="&lt;function1&gt;"> 

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 ...

+4
source share
2 answers

You do not need to add javascript routes. Just add a default parameter to your route entry, e.g.

GET /autocompleteSearch controllers.Application.autocompleteSearch(term: String ?= "")

He will work

+1
source

This question needs to be reformulated (the word autocomplete in the title is misleading) and modified (you are probably looking for playframework-2.0 rather than playback ).

Regarding reverse routing, there seems to be insufficient training tutorials on how to go through, however there is a sample project provided by Play! the team that should catch you.

0
source

Source: https://habr.com/ru/post/1401981/


All Articles