Last active
December 18, 2015 05:19
-
-
Save Jegp/5732033 to your computer and use it in GitHub Desktop.
Using JS routes in Playframework 2.x (Scala)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(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