Skip to content

Instantly share code, notes, and snippets.

@hinrik
Created February 25, 2012 17:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinrik/1909544 to your computer and use it in GitHub Desktop.
Save hinrik/1909544 to your computer and use it in GitHub Desktop.
A better Rails confirmation dialog
# Replace Rails' confirmation dialog with a translatable jQuery UI modal dialog.
# Requires i18n-js (https://github.com/fnando/i18n-js)
# Translation strings used: confirm.title, confirm.ok, confirm.cancel
$ ->
$.rails.confirm = (message) ->
$("#confirm_dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + message + '</p>')
false
if $('[data-confirm]')
$('body').append("<div id='confirm_dialog' title='" + I18n.t('confirm.title') + "'></div>")
diag_elem = $('#confirm_dialog')
diag_elem.dialog
autoOpen: false
modal: true
open: ->
$('button:first').blur() # prevent jQuery from focusing it
$('[data-confirm]').each ->
link_elem = $(this)
method = link_elem.attr('data-method')
method = 'get' unless method?
link_elem.click (e) ->
e.preventDefault()
diag_btns = {}
diag_btns[I18n.t('confirm.ok')] = ->
$.ajax
type: 'post'
async: false
url: link_elem.attr('href')
data: { _method: method }
dataType: 'html'
success: (x) -> $('body').html(x)
diag_elem.dialog('close')
diag_btns[I18n.t('confirm.cancel')] = ->
diag_elem.dialog('close')
diag_elem.dialog(buttons: diag_btns)
diag_elem.dialog('open')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment