0
votes

I am trying to make some Javascript Rounting in my Play Framework app and am running into some errors:

I have a button with the id #next which should use Ajax to load some new image and text into a div named #content.

In my Application.java I have implemented the following two methods:

public static Result nextUser(int i) {
    return ok(content.render(users.get(i)));
}

public static Result javascriptRoutes() {
    response().setContentType("text/javascript");
    return ok(
            Routes.javascriptRouter("jsRoutes",
                    controllers.routes.javascript.Application.nextUser()
            )
    );
}

Made this content.scala.html file:

@(user: User)
<img id="user" src="@routes.Assets.at(user.getImg())" />
<h2>@user.getStatus()</h2>

Added this into to the routes file:

GET        /nextuser                    controllers.Application.nextUser(i: Int)

GET        /assets/javascripts/routes   controllers.Application.javascriptRoutes()

And then tried this Javascript code:

<script type="text/javascript" src="@routes.Application.javascriptRoutes()"></script>
<script>
    var userIndex = 1;

    $("#next").click(function() {
        jsRoutes.controllers.Application.nextUser(userIndex).ajax({
        success : function(data) {
            $("#content").html(data);
        },
        error: function(err) {

        }
        });
    });

</script>

In the Application.java file in the javascriptRoutes Method the controllers.routes. can not be found.

Does anybody know what is causing the error?

Edit:

I could solve the initial error thanks to this: Unable to resolve reverse routing methods in IntelliJ

Now it looks like this:

enter image description here

The nextUser Method cannot be found even when cleary defined.

When I compile the application using activator compile and run it using activator run my chrome console looks like this:

http://i.gyazo.com/34581c81beda3520f927991c6a0c554f.png

so still the routes file somehow cant be found.

1
Why scala is tagged? - curious
Could you maybe try to explain better what you are trying to achieve? - Martijn
Use the JavaScriptReverseRouter as described in the docs: playframework.com/documentation/2.5.x/… -- note that the embedded router does not with methods having parameters; you really have to use the JavaScriptReverseRouter. - koppor

1 Answers

0
votes

Ok I could fix the problem thanks to this:

Can't get javascriptRoutes to work with Play Framework 2

The problem was the order in my routes document. As soon as I switched the assets declaration before the javascript routes it worked:

# Javascript Router
GET        /assets/javascripts/routes        controllers.Application.javascriptRoutes()

# Map static resources from the /public folder to the /assets URL path
GET        /assets/*file                controllers.Assets.at(path="/public", file)