Less Verbose way of generating Play 2's javascript router

前端 未结 5 870
北海茫月
北海茫月 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:07

    Also, if you are using Play 2.4, some Classes/packages have been changed:

    def javascriptRoutes = Action { implicit request =>
      Ok(play.api.routing.JavaScriptReverseRouter("jsRoutes")(routeCache:_*)).as("text/javascript")
    }
    
    val routeCache: Array[JavaScriptReverseRoute] = {
      import routes._
      val jsRoutesClass: Class[javascript] = classOf[routes.javascript]
      val controllers = jsRoutesClass.getFields.map(_.get(null))
      val met = for (
        controller <- controllers;
        method <- controller.getClass.getDeclaredMethods if method.getReturnType == classOf[play.api.routing.JavaScriptReverseRoute]
      ) yield method.invoke(controller).asInstanceOf[play.api.routing.JavaScriptReverseRoute]
      met
    }
    

提交回复
热议问题