0
votes

I´m having a curious problem in my application when I use the Javascript Routing of Play Framework. This is the problem:

Screenshot

and this is my code:

    class Application extends Controller {

      import play.api.mvc._
      import play.api.routing._

      def javascriptRoutes = Action { implicit request =>
        Ok(
          JavaScriptReverseRouter("jsRoutes")(routes.javascript.ProcessController.retrieveAllProcess)
        ).as("text/javascript")
      }
    }

this is muy route file:

    GET  /javascriptRoutes  controllers.Application.javascriptRoutes
    GET /Process/All        controllers.ProcessController.retrieveAllProcess

this is the html.scala file:

    var option =
        {   "url" : @routes.javascript.ProcessController.retrieveAllProcess,
         ...
       }

Thank you so much :).

1

1 Answers

1
votes

I think you're confusing the javascript routing with reverse routing.

Javascript routing

Javascript routing is meant to generate javascript code. The generated code defines functions that can be used in your javascript to generate urls at the client side. Since you've defined an Action returning the generated code, you could include it in your page by using

<script type="text/javascript" src="@routes.Application.retrieveAllProcess()"></script>

Note that this uses reverse routing to tell the html page which URL should be used for the script. :)

Then in your javascript code it can be used like so

var retrieveAllProcessRoute = jsRoutes.controllers.Application.retrieveAllProcess()

to generate a javascript object containing both the HTTP verb and url to the Application::retrieveAllProcess method.

Reverse routing

Reverse routing is used to generate scala code. This code is meant to generate URLs in views or Redirects to controller methods on the server side.

Since you're talking about a template (*.html.scala), you're talking about the server side of things. This means that you should use the reverse routing capabilities of Play. Below I've rewritten your javascript template snippet to use reverse routing:

var option = {
  "url": @routes.Application.retrieveAllProcess,
  ...
}