Skip to content

Instantly share code, notes, and snippets.

@davydotcom
Last active January 2, 2016 11:59
Show Gist options
  • Save davydotcom/8300124 to your computer and use it in GitHub Desktop.
Save davydotcom/8300124 to your computer and use it in GitHub Desktop.
Grails RESTFUL Delete Link
linkClickSelector = 'a[data-confirm], a[data-method], a[data-remote]'
initializeUJS = ->
console.log "Initializing UJS"
$(document).delegate linkClickSelector, 'click.grails', linkClickHandler
linkClickHandler = (e) ->
link = $(this)
if (!allowAction(link))
e.stopImmediatePropagation()
return false
if link.data('method')
handleMethod(link)
return false
fireEvent = (object, name, data) ->
event = $.Event(name)
object.trigger event, data
return event.result != false
allowAction = (element) ->
message = element.data 'confirm'
answer = false
callback = false
if !message
return true
if fireEvent(element, 'confirm')
answer = confirm(message)
callback = fireEvent(element, 'confirm:complete', [answer])
return answer && callback
handleMethod = (link) ->
method = link.data('method')
href = link.attr('href')
target = link.attr('target')
form = $('<form method="post" action="' + href + '"></form>')
form = $('<form method="' + method + '" action="' + href + '"></form>')
# metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';
if target
form.attr('target',target)
# append(metadataInput)
form.hide().appendTo('body');
form.submit()
initializeUJS()
<g:link controller="permalinks" action="delete"
namespace="spud_admin" method="POST"
data-method="post" id="${permalink.id}"
data-confirm="Are you sure you want to remove this permalink?"
class="btn btn-danger">Remove</g:link>
class SpudPermalinksUrlMappings {
static mappings = {
"/spud/admin/permalinks"(resources: 'permalinks', namespace: 'spud_admin')
"/spud/admin/permalinks/$id/delete"(controller: 'permalinks', action: 'delete', method: 'POST', namespace: 'spud_admin')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment