Artem Panchoyan Artem Panchoyan - 1 year ago 77
Javascript Question

Play 2.x: How to use two jsRoutes files in one scala template

I'm using jsRoutes in my Play 2.1.x app. Part of my routes file looks the following way:

GET /assets/template/js/routes/admin.js controllers.Admin.jsRoutes
GET /assets/template/js/routes/salonManagement.js controllers.SalonManagement.jsRoutes

And I would like to use both references in my scala template (that is by design, one controller contains necessary api functions, the other one necessary form submission urls).
So in my scala template I have the following part:

<script type="text/javascript" src="@routes.Admin.jsRoutes()"></script>
<script type="text/javascript" src="@routes.SalonManagement.jsRoutes()"></script>

Unfortunately, each generated javascript file starts with
var jsRoutes = {};
. Therefore,
overrides properties of
and I can use only the last jsRoutes object.

Now, I know only one workaround. After each jsRoutes declaration I can insert a script that copies old jsRoutes object to a temporary object and then extends new jsRoutes with itself. But that doesn't look like the right way to go.

Isn't there any better way?

Answer Source

There's nothing special about the "jsRoutes" name. You can keep the same method name for consistency among the various controllers, but just pass a different name to the Routes.javascriptRouter method.