Skip to content

Instantly share code, notes, and snippets.

@Jegp
Last active December 18, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jegp/5732033 to your computer and use it in GitHub Desktop.
Save Jegp/5732033 to your computer and use it in GitHub Desktop.
Using JS routes in Playframework 2.x (Scala)
package controllers
object MyController extends Controller {
def add() = Action { implicit request =>
Form( tuple ( "user" -> number ) ).bindFromRequest.fold(
errors => BadRequest,
t => {
Ok("Add id: " + t)
}
)
}
def delete(input : String) = Action { implicit request =>
try {
val id = java.lang.Long.parseLong(input)
Ok("Delete id: " + id)
} catch { case _ : Throwable => BadRequest }
}
}
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# ... Normal routes
# JS Routes:
PUT /comment/add controllers.MyController.add
DELETE /comment/:id controllers.MyController.delete(id)
@(id : Int)
@helper.javascriptRouter("myRoutes")(
routes.javascript.MyController.add,
routes.javascript.MyController.delete
)
<script type="text/javascript">
function addSomeAjax() {
myRoutes.controllers.MyController.add().ajax({
data: { user: @id },
success: function(data) {
alert("Succes!111one");
}
})
}
function deleteSomeAjax() {
myRoutes.controllers.MyController.delete(@id).ajax({
success: function(data) {
alert(data);
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment