public
Last active

Changing default Rails 3 confirmation alert behaviour with a click-twice approach

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// This goes in application.js
// Using this, the confirmation alerts on Rails 3.1 will be replaced with this behaviour:
// The link text changes to 'Sure?' for 2 seconds. If you click the button again within 2 seconds the action is performed,
// otherwise the text of the link (or button) flips back and nothing happens.
 
$.rails.confirm = function(message, element)
{
var state = element.data('state');
var txt = element.text();
if (!state)
{
element.data('state', 'last');
element.text('Sure?');
setTimeout(function()
{
element.data('state', null);
element.text(txt);
}, 2000);
return false;
}
else
{
return true;
}
};
 
$.rails.allowAction = function(element)
{
var message = element.data('confirm'),
answer = false, callback;
if (!message) { return true; }
 
if ($.rails.fire(element, 'confirm'))
{
// le extension.
answer = $.rails.confirm(message, element);
callback = $.rails.fire(element, 'confirm:complete', [answer]);
}
return answer && callback;
};
 
$.rails.handleLink = function(link)
{
if (link.data('remote') !== undefined)
{
$.rails.handleRemote(link);
}
else if (link.data('method'))
{
$.rails.handleMethod(link);
}
 
return false;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.