Custom confirmation dialog in Rails
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
# Coffeescript | |
$.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') | |
### Boostrap | |
$.rails.showConfirmDialog = (link) -> | |
message = link.attr 'data-confirm' | |
html = """ | |
<div class="modal" id="confirmationDialog"> | |
<div class="modal-header"> | |
<a class="close" data-dismiss="modal">Ã</a> | |
<h3>Are you sure Mr. President?</h3> | |
</div> | |
<div class="modal-body"> | |
<p>#{message}</p> | |
</div> | |
<div class="modal-footer"> | |
<a data-dismiss="modal" class="btn">Cancel</a> | |
<a data-dismiss="modal" class="btn btn-primary confirm">OK</a> | |
</div> | |
</div> | |
""" | |
$(html).modal() | |
$('#confirmationDialog .confirm').on 'click', -> $.rails.confirmed(link) | |
### jQueryUI | |
$.rails.showConfirmDialog = (link) -> | |
message = link.attr 'data-confirm' | |
html = """ | |
<div id="dialog-confirm" title="Are you sure Mr. President?"> | |
<p>#{message}</p> | |
</div> | |
""" | |
$(html).dialog | |
resizable: false | |
modal: true | |
buttons: | |
OK: -> | |
$.rails.confirmed link | |
$(this).dialog "close" | |
Cancel: -> | |
$(this).dialog "close" | |
### Noty (http://needim.github.com/noty/) | |
$.rails.showConfirmDialog = (link) -> | |
message = link.attr 'data-confirm' | |
okButton = | |
type: 'btn btn-primary' | |
text: 'Ok' | |
click: (noty) -> $.rails.confirmed(link); noty.close() | |
cancelButton = | |
type: 'btn btn-danger' | |
text: 'Cancel' | |
click: (noty) -> noty.close() | |
noty | |
text: message | |
buttons: [okButton, cancelButton] | |
closable: false | |
timeout: false | |
modal: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment