Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created Oct 29, 2012
Embed
What would you like to do?
Custom confirmation dialog in Rails
# 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