Created
May 7, 2015 19:40
-
-
Save hugomaiavieira/f6cf3d1d413126e8d58c to your computer and use it in GitHub Desktop.
Js to use bootbox with links
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
//= require bootbox.min | |
// | |
// Open a confirm modal when click in a link. | |
// | |
// Dependencies: JQuery and http://bootboxjs.com | |
// | |
// Usage: | |
// | |
// Add attributes data-confirm-title and data-confirm-message to a link tag or | |
// on a form tag. There is a problem data you MUST add a data-method too, even | |
// if it is a GET. | |
// | |
$(function () { | |
bootbox.addLocale('br', { | |
OK : 'Ok', | |
CANCEL : 'Cancelar', | |
CONFIRM : 'Confirmar' | |
}); | |
bootbox.setDefaults({ locale: 'br' }); | |
function bootboxConfirm (e, options) { | |
var element = $(e.currentTarget); | |
// this is to prevent click when using rails jquery-ujs disable_with | |
if (element.data('ujs:enableWith')) return false ; | |
options = options || {}; | |
if ( !options.confirmed ) { | |
e.stopImmediatePropagation(); | |
var title = element.data('confirm-title') || '', | |
message = element.data('confirm-message') || ''; | |
bootbox.confirm({ | |
title: title, | |
message: message, | |
callback: function(result) { | |
if (result) | |
element.trigger(e.type, { confirmed: true }); | |
} | |
}); | |
// the same as e.stopPropagation() and e.preventDefault() | |
return false; | |
} | |
// else { normal event flow } | |
} | |
$('a[data-confirm-message]').on('click', bootboxConfirm); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment