polo
polo

Reputation: 1452

How to create a generic hook for an Action in Play framework?

I want to to check if the request has a query string param called callback, and if it does, I want to wrap the response in a function call (for jsonp).

I am new to both scala and play, so I'm not sure what's the best way to do that, avoiding code duplication.

Upvotes: 1

Views: 265

Answers (1)

yan
yan

Reputation: 20982

You can use ActionBuilder to create a custom Action.

For example,

import play.api.mvc._

object CallbackAction extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[SimpleResult]) = {
    request.getQueryString("callback").map { callback =>
      // invoke the function identified by callback
    }
    block(request)
  }
}

Upvotes: 2

Related Questions