Created
November 19, 2014 17:57
-
-
Save abstractcoder/49a2b70482ec7c4dfb28 to your computer and use it in GitHub Desktop.
Custom Rails confirmation dialog with SweetAlert
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
jQuery(function() { | |
// Override rails confirm function | |
$.rails.confirm = function(msg) { return true; } | |
// Unbind any click handlers (optional), create custom handler | |
$('[data-confirm]').unbind('click').click(function(evt){ | |
// Get the target | |
var $target = $(this); | |
// Check if the action has already been confirmed | |
if ($target.data('confirmed') == true) { | |
// If so remove confirmation flag | |
$target.removeData('confirmed'); | |
// Continue the event | |
return true; | |
} | |
// The action hasn't been confirmed | |
else { | |
// Prevent the event from firing | |
evt.preventDefault(); | |
// Popup the sweet alert confirmation dialog | |
sweetAlert( | |
{ | |
title: "", | |
text: $target.data('confirm'), | |
type: "warning", | |
showCancelButton: true | |
}, | |
// Confirmation call back | |
function(confirmed) { | |
// If confirmed | |
if (confirmed) { | |
// Add a confirmed flag | |
$target.data('confirmed', true); | |
// Trigger the click event | |
$target.trigger('click'); | |
} | |
} | |
); | |
// Stop the event from propagating | |
return false; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment