Last active
December 13, 2015 17:38
-
-
Save sanjayginde/4949031 to your computer and use it in GitHub Desktop.
Implementation of custom, async confirmation dialog with Rails UJS
This file contains 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
;(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