Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for adding javascript in included views #29

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sample/app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
53 changes: 53 additions & 0 deletions sample/app/controllers/JavascriptPage.scala
Original file line number Diff line number Diff line change
@@ -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]
}
}
}
15 changes: 15 additions & 0 deletions sample/app/views/indexBody.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import controllers.JavascriptPage

@()(implicit request: Request[_])

@JavascriptPage.addNonBlockingJs {
<script>
window.alert("Script 1");
</script>
}

@JavascriptPage.addNonBlockingJs {
<script>
window.alert("Script 2");
</script>
}
7 changes: 7 additions & 0 deletions sample/app/views/inlineNonBlockingJs.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import controllers.JavascriptPage

@()(implicit request: Request[_])

@defining(JavascriptPage.getNonBlockingJs()) { scripts =>
@scripts.map { item => @item }
}
4 changes: 4 additions & 0 deletions sample/app/views/js.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@(implicit request: Request[_])
@main("Welcome to") {
@indexBody()
}
2 changes: 2 additions & 0 deletions sample/app/views/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
</head>
<body>
@content

@inlineNonBlockingJs()
</body>
</html>
1 change: 1 addition & 0 deletions sample/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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)