Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:22:36+00:00 2026-06-10T01:22:36+00:00

Currently I define my app’s javascript router in a fairly verbose way def javascriptRoutes

  • 0

Currently I define my app’s javascript router in a fairly verbose way

def javascriptRoutes = Action { implicit request =>
  import routes.javascript._
  Ok(Routes.javascriptRouter("jsRoutes")(
    Login.method1,Login.Method2,
    OtherController.method1,OtherController.method2,
    //[...]
  )).as("text/javascript")
}

What I really would like to do is to create a javascriptRouter with all of the routes in the routes file, so I don’t have to manually update the javascriptRoutes definition each time I add a new controller method.

Is there a way to accomplish this task, or is there even a slightly less verbose way of defining the javascriptRouter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:22:38+00:00Added an answer on June 10, 2026 at 1:22 am

    You can do it via reflection like so:

    val routeCache = {
        import routes._
        val jsRoutesClass = classOf[routes.javascript]
        val controllers = jsRoutesClass.getFields().map(_.get(null))
        controllers.flatMap { controller =>
            controller.getClass().getDeclaredMethods().map { action =>
                action.invoke(controller).asInstanceOf[play.core.Router.JavascriptReverseRoute]
            }
        }
    }
    
    def javascriptRoutes = Action { implicit request =>
        Ok(Routes.javascriptRouter("jsRoutes")(routeCache:_*)).as("text/javascript")
    }
    

    This was derived from the generated source files found in target/scala-2.x.x/src_managed. You could actually add your own source generator and parse the routes file yourself, but I find doing it via reflection easier.

    An additional thing you might want to do is filter out the methods you don’t want as this will give you ALL the routes (including the javascriptRouter itself).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, if we define our view as {{#view App.myView}} , ember/handlebars will wrap the
Currently, I define several theme in my app R.style.Theme_MyApp_Light R.style.Theme_MyApp_Dark User has the option
Our database currently doesn't define primary keys on any tables. All of the id
Currently the HashSet<T> constructor that allows you to define your equality comparison yourself is
A C++ beginner's question. Here is what I have currently: // From tchar.h #define
My grails app has to define some additional behaviour for many of the standard
I'm currently on Dreamhost attempting to run a Rails 2.3.5 app. Here is the
now I am writing a app in C++, and currently my app reads models
I am currently creating an iPhone app using Xcode 4.0.2, I want the app
Is it possible to define a MBean with an array attribute. I currently have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.