Created
September 6, 2014 09:50
-
-
Save rubencp/b7152523532b454eb0c0 to your computer and use it in GitHub Desktop.
Override Confirm dialog Rails 4 and Bootbox.js
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 |
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
gem 'bootbox-rails', '~>0.3' |
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
<a href="#" id="link">Click me to verify that it bootbox is working</a> | |
<script> | |
$(function() { | |
$('#link').click(function() { | |
bootbox.alert("Hello world!"); | |
}) | |
}) | |
</script> | |
<%= link_to 'Deleta', admin_path(user), :method => :delete, data: { confirm: "Are you 1000% Sure?" }, :class => "btn pull-right btn-danger btn-xs" %> |
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
var ready; | |
ready = function() { | |
window.myCustomConfirmBox = function(message, callback) { | |
return bootbox.dialog({ | |
message: message, | |
"class": 'class-confirm-box', | |
className: "my-modal", | |
buttons: { | |
success: { | |
label: "More than Sure, Thankyou!", | |
className: "btn-danger", | |
callback: function() { | |
return callback(); | |
} | |
}, | |
chickenout: { | |
label: "No, I'll chicken out!", | |
className: "btn-success pull-left" | |
} | |
} | |
}); | |
}; | |
return $.rails.allowAction = function(element) { | |
var answer, callback, message; | |
message = element.data("confirm"); | |
if (!message) { | |
return true; | |
} | |
answer = false; | |
callback = void 0; | |
if ($.rails.fire(element, "confirm")) { | |
myCustomConfirmBox(message, function() { | |
var oldAllowAction; | |
callback = $.rails.fire(element, "confirm:complete", [answer]); | |
if (callback) { | |
oldAllowAction = $.rails.allowAction; | |
$.rails.allowAction = function() { | |
return true; | |
}; | |
element.trigger("click"); | |
return $.rails.allowAction = oldAllowAction; | |
} | |
}); | |
} | |
return false; | |
}; | |
}; | |
$(document).ready(ready); | |
$(document).on('page:load', ready); |
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
ready = -> | |
window.myCustomConfirmBox = (message,callback) -> | |
bootbox.dialog | |
message: message | |
class: 'class-confirm-box' | |
className: "my-modal" | |
buttons: | |
success: | |
label: "More than Sure, Thankyou!" | |
className: "btn-danger" | |
callback: -> callback() | |
chickenout: | |
label: "No, I'll chicken out!" | |
className: "btn-success pull-left" | |
$.rails.allowAction = (element) -> | |
message = element.data("confirm") | |
return true unless message | |
answer = false | |
callback = undefined | |
if $.rails.fire(element, "confirm") | |
myCustomConfirmBox message, -> | |
callback = $.rails.fire(element, "confirm:complete", [answer]) | |
if callback | |
oldAllowAction = $.rails.allowAction | |
$.rails.allowAction = -> | |
true | |
element.trigger "click" | |
$.rails.allowAction = oldAllowAction | |
false | |
$(document).ready(ready) | |
$(document).on('page:load', ready) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment