diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-02-04 18:01:08 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-02-04 18:01:08 -0800 |
commit | 7b1d3a0ca38c480ef65e78d28f2b18573b8abd74 (patch) | |
tree | 58ebb8ff0882cfda2dc10f1f6914453e3b601759 /railties | |
parent | 6bf79f02bede6a75a5211de64a1359b10749a2df (diff) | |
download | rails-7b1d3a0ca38c480ef65e78d28f2b18573b8abd74.tar.gz rails-7b1d3a0ca38c480ef65e78d28f2b18573b8abd74.tar.bz2 rails-7b1d3a0ca38c480ef65e78d28f2b18573b8abd74.zip |
Add prototype ujs handler for data-method
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/generators/rails/app/templates/public/javascripts/rails.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/railties/lib/generators/rails/app/templates/public/javascripts/rails.js b/railties/lib/generators/rails/app/templates/public/javascripts/rails.js index 8e2e0d259d..f7ddba390a 100644 --- a/railties/lib/generators/rails/app/templates/public/javascripts/rails.js +++ b/railties/lib/generators/rails/app/templates/public/javascripts/rails.js @@ -1,4 +1,11 @@ document.observe("dom:loaded", function() { + var authToken = $$('meta[name=csrf-token]').first().readAttribute('content'), + authParam = $$('meta[name=csrf-param]').first().readAttribute('content'), + formTemplate = '<form method="#{method}" action="#{action}">\ + #{realmethod}<input name="#{param}" value="#{token}" type="hidden">\ + </form>', + realmethodTemplate = '<input name="_method" value="#{method}" type="hidden">'; + function handleRemote(element) { var method, url, params; @@ -45,6 +52,25 @@ document.observe("dom:loaded", function() { handleRemote(element); event.stop(); } + + var element = event.findElement("a[data-method]"); + if (element && element.readAttribute('data-remote') != 'true') { + var method = element.readAttribute('data-method'), + piggyback = method.toLowerCase() != 'post', + formHTML = formTemplate.interpolate({ + method: 'POST', + realmethod: piggyback ? realmethodTemplate.interpolate({ method: method }) : '', + action: element.readAttribute('href'), + token: authToken, + param: authParam + }); + + var form = new Element('div').update(formHTML).down().hide(); + this.insert({ bottom: form }); + + form.submit(); + event.stop(); + } }); // TODO: I don't think submit bubbles in IE |