Skip to content

Instantly share code, notes, and snippets.

@hugomaiavieira
Created May 7, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugomaiavieira/f6cf3d1d413126e8d58c to your computer and use it in GitHub Desktop.
Save hugomaiavieira/f6cf3d1d413126e8d58c to your computer and use it in GitHub Desktop.
Js to use bootbox with links
//= 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