Fix Ajax processing.

This commit is contained in:
takezoe
2013-06-28 02:47:02 +09:00
parent fb11cd6f73
commit bb863962c0
3 changed files with 60 additions and 23 deletions

View File

@@ -27,8 +27,50 @@ abstract class ControllerBase extends ScalatraFilter
}
}
protected def NotFound() = html.error("Not Found")
protected def Unauthorized() = redirect("/")
def ajaxGet(path : String)(action : => Any) : Route = {
super.get(path){
request.setAttribute("AJAX", "true")
action
}
}
override def ajaxGet[T](path : String, form : MappingValueType[T])(action : T => Any) : Route = {
super.ajaxGet(path, form){ form =>
request.setAttribute("AJAX", "true")
action(form)
}
}
def ajaxPost(path : String)(action : => Any) : Route = {
super.post(path){
request.setAttribute("AJAX", "true")
action
}
}
override def ajaxPost[T](path : String, form : MappingValueType[T])(action : T => Any) : Route = {
super.ajaxPost(path, form){ form =>
request.setAttribute("AJAX", "true")
action(form)
}
}
protected def NotFound() = {
if(request.getAttribute("AJAX") == null){
org.scalatra.NotFound(html.error("Not Found"))
} else {
org.scalatra.NotFound()
}
}
// TODO redirect to the sign-in page if not logged in?
protected def Unauthorized() = {
if(request.getAttribute("AJAX") == null){
org.scalatra.Unauthorized(redirect("/"))
} else {
org.scalatra.Unauthorized()
}
}
protected def baseUrl = {
val url = request.getRequestURL.toString

View File

@@ -32,7 +32,7 @@ trait LabelsControllerBase extends ControllerBase {
redirect("/%s/%s/issues".format(owner, repository))
})
get("/:owner/:repository/issues/label/edit")(writableRepository {
ajaxGet("/:owner/:repository/issues/label/edit")(writableRepository {
val owner = params("owner")
val repository = params("repository")
@@ -40,7 +40,7 @@ trait LabelsControllerBase extends ControllerBase {
.map(issues.html.labeleditlist(getLabels(owner, repository), _)) getOrElse NotFound()
})
get("/:owner/:repository/issues/label/:labelId/edit")(writableRepository {
ajaxGet("/:owner/:repository/issues/label/:labelId/edit")(writableRepository {
val owner = params("owner")
val repository = params("repository")
val labelId = params("labelId").toInt
@@ -50,7 +50,7 @@ trait LabelsControllerBase extends ControllerBase {
} getOrElse NotFound()
})
post("/:owner/:repository/issues/label/:labelId/edit", editForm)(writableRepository { form =>
ajaxPost("/:owner/:repository/issues/label/:labelId/edit", editForm)(writableRepository { form =>
val owner = params("owner")
val repository = params("repository")
val labelId = params("labelId").toInt
@@ -61,7 +61,7 @@ trait LabelsControllerBase extends ControllerBase {
} getOrElse NotFound()
})
get("/:owner/:repository/issues/label/:labelId/delete")(writableRepository {
ajaxGet("/:owner/:repository/issues/label/:labelId/delete")(writableRepository {
val owner = params("owner")
val repository = params("repository")
val labelId = params("labelId").toInt