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:
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:
so still the routes file somehow cant be found.
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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments