Skip to content

kipsigman/play-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

play-extensions

Additional functionality for use with Play Framework apps.

Feature Overview

Install

Add the following to your build.sbt file:

resolvers += Resolver.bintrayRepo("kipsigman", "maven")

libraryDependencies += "kipsigman" %% "play-extensions" % "0.3.3"

JavaScript resources

  1. Extend or mixin the JavaScriptResourceController
import scala.concurrent.ExecutionContext

import com.typesafe.config.Config
import javax.inject.Inject
import javax.inject.Singleton
import kipsigman.play.js.JavaScriptMessageService
import kipsigman.play.mvc.JavaScriptResourceController
import play.api.i18n.MessagesApi
import play.api.mvc.Action
import play.api.routing.JavaScriptReverseRoute

@Singleton
class Application @Inject() (
  messagesApi: MessagesApi,
  protected val config: Config,
  protected val javaScriptMessagesService: JavaScriptMessagesService) extends Controller with JavascriptResourceController {
  
  // Set your routes
  override protected def javaScriptReverseRoutes: Seq[JavaScriptReverseRoute] = Seq(
    routes.javascript.Assets.at,
    routes.javascript.Application.someAjaxAction    
  )
  
  def someAjaxAction = Action {
    import play.api.json._
    val json = Json.obj("status" -> "success", "msg" -> "Item saved")
    Ok(json)
  }
}
  1. Add to conf/routes

    # JavaScript resources
    GET  /resources/config.js    controllers.Application.configJs
    GET  /resources/messages.js  controllers.Application.messagesJs
    GET  /resources/routes.js    controllers.Application.routesJs
    
  2. Add scripts to view

    <script type="text/javascript" src="@routes.Application.configJs"></script>
    <script type="text/javascript" src="@routes.Application.messagesJs"></script>
    <script type="text/javascript" src="@routes.Application.routesJs"></script>
    
  3. Use in your JavaScript files

    $.ajax({
      method : "POST",
      // Use routes resource
      url : routes.controllers.Application.someAjaxAction().url,
      dataType : 'json',
      success : function(data, textStatus, jqXHR) {
        if (data.status === "success") {
          // use messages resource
          var msg = messages("ajax.error");
          alert(msg);
        } else if (data.status === "error") {
          var msg = messages("ajax.success");
          alert(msg);
        }
      }
    });
  4. (optional) Customize namespacing and object names Sometimes you may prefer to define namespace and object names to avoid clashing with other JavaScript resources.

    class Application
    ...
    override protected def namespace: Option[String] = Some("myPlayApp")
    
    override protected def configObjectName: String = "theConfig"
    
    var secret = myPlayApp.theConfig.play.crypto.secret;

S3 file & image management

Serve assets from S3

  1. Add configuration for AWS S3

    • Set directly in application.conf/production.conf
    aws {
      accessKeyId=<anaccessid>
      secretKey=<asecretkey>
      s3 {
        bucketName=<abucketname>
      }
    }
    
    • or set environment variables (see reference.conf)
    export AWS_ACCESS_KEY_ID=xxx
    export AWS_SECRET_KEY=yyy
    export AWS_S3_BUCKET_NAME=zzz
  2. Add routes S3Controller

GET  /s3assets/*filename         kipsigman.play.mvc.S3Controller.file(filename)
GET  /s3assets/images/*filename  kipsigman.play.mvc.S3Controller.image(filename)

Image edit/upload

Data/form support for java.time

Contributors/Recognition

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages