As others have mentioned, you cannot use a jQuery dialog box, as $.rails.confirm
needs to block until it returns the users answer.
However, you can overwrite $.rails.allowAction
in your application.js
file like this:
$.rails.allowAction = function(element) {
var message = element.data('confirm'),
answer = false, callback;
if (!message) { return true; }
if ($.rails.fire(element, 'confirm')) {
myCustomConfirmBox(message, function() {
callback = $.rails.fire(element,
'confirm:complete', [answer]);
if(callback) {
var oldAllowAction = $.rails.allowAction;
$.rails.allowAction = function() { return true; };
element.trigger('click');
$.rails.allowAction = oldAllowAction;
}
});
}
return false;
}
function myCustomConfirmBox(message, callback) {
// implement your own confirm box here
// call callback() if the user says yes
}
It works by returning false
immediately, thus effectively canceling the click event. However, your custom function can then call the callback to actually follow the link/submit the form.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…