Skip to content

Instantly share code, notes, and snippets.

@sanjayginde
Last active December 13, 2015 17:38
Show Gist options
  • Save sanjayginde/4949031 to your computer and use it in GitHub Desktop.
Save sanjayginde/4949031 to your computer and use it in GitHub Desktop.
Implementation of custom, async confirmation dialog with Rails UJS
;(function($, document) {
$(document).delegate('a[data-confirm]', 'confirm', function(event) {
event.preventDefault();
var $link = $(this);
// Placeholder for custom confirmation popup, etc
my_custom_dialog_widget.confirm({
message: $link.attr('data-confirm'),
title: "Are you sure?",
onConfirm: function() {
// confirmation accepted
$link.trigger('confirm:complete', true);
},
onCancel: function() {
// confirmation rejected
$link.trigger('confirm:complete', false);
}
});
return false;
});
$(document).delegate('a[data-confirm]', 'confirm:complete', function(event, answer) {
if (answer) {
var $link = $(this);
if ( $link.data('remote') ) {
$.rails.handleRemote($link);
} else {
$.rails.handleMethod($link);
}
}
});
})(jQuery, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment