Created
February 25, 2012 17:08
-
-
Save hinrik/1909544 to your computer and use it in GitHub Desktop.
A better Rails confirmation dialog
This file contains hidden or 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
# 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