Created
May 3, 2013 10:14
-
-
Save mieko/5508348 to your computer and use it in GitHub Desktop.
rails-ujs bootstrap confirm based on: http://rors.org/demos/custom-confirm-in-rails
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
# This implementation replaces rails-ujs's :confirm behavior, from a | |
# browser-provided, window.confirm() to a nice bootstrap dialog. | |
# This is based upon the technique and code described here: | |
# | |
# http://rors.org/demos/custom-confirm-in-rails | |
# | |
# Special handling of delete actions, better tab behavior, | |
# and DOM cleanup was added. | |
$.rails.allowAction = (link) -> | |
return true unless link.attr('data-confirm') | |
$.rails.showConfirmDialog(link) # look bellow for implementations | |
false # always stops the action since code runs asynchronously | |
$.rails.confirmed = (link) -> | |
link.removeAttr('data-confirm') | |
link.trigger('click.rails') | |
$.rails.showConfirmDialog = (link) -> | |
html = """ | |
<div class="modal hide fade" tabindex="-1"> | |
<div class="modal-header" tabindex="-1"> | |
<a class="close" tabindex="-1" data-dismiss="modal"><i class="icon-remove"></i></a> | |
<h3>Are you sure?</h3> | |
</div> | |
<div class="modal-body"> | |
<p>You'ze sure, right, right?</p> | |
</div> | |
<div class="modal-footer"> | |
<button data-dismiss="modal" class="btn cancel">Cancel</button> | |
<button data-dismiss="modal" class="btn btn-primary confirm">OK</button> | |
</div> | |
</div> | |
""" | |
dlg = $(html) | |
# Set our text safely. | |
dlg.find('.modal-body p').text(link.attr('data-confirm')) | |
# If this is a delete action (common), make it read a bit more obviously. | |
if link.attr('data-method') == 'delete' | |
dlg.find('.btn.confirm').removeClass('btn-primary').addClass('btn-danger').text('Delete') | |
dlg.find('.btn.cancel').addClass('btn-inverse').text('No, Keep It.') | |
dlg.modal() | |
dlg.find('.confirm').on 'click', -> | |
$.rails.confirmed(link) | |
# We can't have it just hanging around the DOM... | |
dlg.on 'hidden', -> | |
dlg.remove() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment