diff --git a/sample/app/controllers/Application.scala b/sample/app/controllers/Application.scala index f5ee060..0197a68 100644 --- a/sample/app/controllers/Application.scala +++ b/sample/app/controllers/Application.scala @@ -21,5 +21,9 @@ object Application extends Controller with DBSessionElement with LoggingElement val messages = Message.findAll Ok(views.html.messages(messages)) } + + def javascriptLateBinding = StackAction { implicit req => + Ok(views.html.js) + } } diff --git a/sample/app/controllers/JavascriptPage.scala b/sample/app/controllers/JavascriptPage.scala new file mode 100644 index 0000000..fe78b81 --- /dev/null +++ b/sample/app/controllers/JavascriptPage.scala @@ -0,0 +1,53 @@ +package controllers + +import jp.t2v.lab.play2.stackc.{RequestAttributeKey, RequestWithAttributes} +import play.api.mvc.Request +import play.twirl.api.{Html, HtmlFormat} + +object JavascriptPage { + + case class NonBlockingJs(uid: Int) extends RequestAttributeKey[Html] + protected object NonBlockingJsCount extends RequestAttributeKey[Int] + case class BlockingJs(uid: Int) extends RequestAttributeKey[Html] + protected object BlockingJsCount extends RequestAttributeKey[Int] + + def addNonBlockingJs(jscript: Html)(implicit request: Request[_]): Html = { + request match { + case i: RequestWithAttributes[_] => + val uid = i.get(NonBlockingJsCount).getOrElse(0) + i.set(NonBlockingJs(uid), jscript) + i.set(NonBlockingJsCount,uid+1) + case _ => + } + + HtmlFormat.empty + } + + def addBlockingJs(jscript: Html)(implicit request: Request[_]): Html = { + request match { + case i: RequestWithAttributes[_] => + val uid = i.get(BlockingJsCount).getOrElse(0) + i.set(BlockingJs(uid), jscript) + i.set(BlockingJsCount,uid+1) + case _ => + } + HtmlFormat.empty + } + + def getNonBlockingJs()(implicit request: Request[_]): Seq[Html] = { + request match { + case i: RequestWithAttributes[_] => + i.get(NonBlockingJsCount) match { + case Some(maxUid) => + (0 until maxUid).map { uid => + i.get(NonBlockingJs(uid)) match { + case None => HtmlFormat.empty + case Some(jscript) => jscript + } + } + case _ => Seq.empty[Html] + } + case _ => Seq.empty[Html] + } + } +} diff --git a/sample/app/views/indexBody.scala.html b/sample/app/views/indexBody.scala.html new file mode 100644 index 0000000..d13ab9a --- /dev/null +++ b/sample/app/views/indexBody.scala.html @@ -0,0 +1,15 @@ +@import controllers.JavascriptPage + +@()(implicit request: Request[_]) + +@JavascriptPage.addNonBlockingJs { + +} + +@JavascriptPage.addNonBlockingJs { + +} diff --git a/sample/app/views/inlineNonBlockingJs.scala.html b/sample/app/views/inlineNonBlockingJs.scala.html new file mode 100644 index 0000000..97c899a --- /dev/null +++ b/sample/app/views/inlineNonBlockingJs.scala.html @@ -0,0 +1,7 @@ +@import controllers.JavascriptPage + +@()(implicit request: Request[_]) + +@defining(JavascriptPage.getNonBlockingJs()) { scripts => + @scripts.map { item => @item } +} diff --git a/sample/app/views/js.scala.html b/sample/app/views/js.scala.html new file mode 100644 index 0000000..4887fa2 --- /dev/null +++ b/sample/app/views/js.scala.html @@ -0,0 +1,4 @@ +@(implicit request: Request[_]) +@main("Welcome to") { + @indexBody() +} diff --git a/sample/app/views/main.scala.html b/sample/app/views/main.scala.html index 6c72d80..2bf19c2 100644 --- a/sample/app/views/main.scala.html +++ b/sample/app/views/main.scala.html @@ -11,5 +11,7 @@ @content + + @inlineNonBlockingJs() diff --git a/sample/conf/routes b/sample/conf/routes index b42987b..96c64d6 100644 --- a/sample/conf/routes +++ b/sample/conf/routes @@ -8,6 +8,7 @@ GET / controllers.Application.index GET /messages controllers.Application.messages GET /messages/:id controllers.Application.editMessage(id: Int) +GET /javascript controllers.Application.javascriptLateBinding # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)