Created
February 4, 2014 11:38
-
-
Save unnitallman/8802165 to your computer and use it in GitHub Desktop.
Rails UJS extension to add 'prompt'
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
/* | |
How to use: | |
%= link_to 'link text', url, data: {prompt: 'Enter your password', param-name: 'password', method: :delete} | |
*/ | |
RailsJSPrompt = { | |
prompt: function(l){ | |
var link = $(l); | |
var href = link.attr('href'), | |
method = link.data('method'), | |
target = link.attr('href'), | |
csrfToken = $('meta[name=csrf-token]').attr('content'), | |
csrfParam = $('meta[name=csrf-param]').attr('content'), | |
form = $('<form method="post" action="' + href + '"></form>'), | |
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />', | |
msg = link.data('prompt'), | |
value = prompt(msg), | |
paramName = link.data('param-name') || 'prompt-value', | |
promptParamInput = '<input name="' + paramName + '" value="' + value + '" type="hidden" />'; | |
if (csrfParam !== undefined && csrfToken !== undefined) { | |
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />'; | |
} | |
form.hide().append(metadataInput).append(promptParamInput).appendTo('body'); | |
form.submit(); | |
} | |
} | |
$('a[data-prompt]').on('click', function(e) { | |
e.stopImmediatePropagation(); | |
e.preventDefault(); | |
RailsJSPrompt.prompt(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment