Less Verbose way of generating Play 2's javascript router

前端 未结 5 876
北海茫月
北海茫月 2021-02-06 00:54

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

def javascriptRoutes = Action { implicit request =>
  import routes.javascript._
  Ok(R         


        
5条回答
  •  不知归路
    2021-02-06 01:21

    Very nice solution by thatsmydoing. If you have the JavaScript-routes under some other subpackage, you need to declare routeCache like this

    val routeCache = {
      val jsRoutesClass = classOf[controllers.api.routes.javascript]
      val controllerArray = jsRoutesClass.getFields().map(_.get(null))
      controllerArray.flatMap { controller =>
        controller.getClass().getDeclaredMethods().map { action =>
        action.invoke(controller).asInstanceOf[play.core.Router.JavascriptReverseRoute]
      }
     }
    }
    

提交回复
热议问题