Skip to content

Instantly share code, notes, and snippets.

@khash
Created May 4, 2012 11:59
Show Gist options
  • Save khash/2594409 to your computer and use it in GitHub Desktop.
Save khash/2594409 to your computer and use it in GitHub Desktop.
Changing default Rails 3 confirmation alert behaviour with a click-twice approach
// 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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment