Skip to content
Browse files

Carry over target attribute for a[data-method] links to form. Closes

  • Loading branch information...
1 parent 44df81a commit 888be8adcb7d609f442b3883dc722c5427e49354 @JangoSteve JangoSteve committed Oct 3, 2011
Showing with 23 additions and 8 deletions.
  1. +3 −0 src/rails.js
  2. +17 −6 test/public/test/data-method.js
  3. +3 −2 test/public/test/settings.js
View
3 src/rails.js
@@ -159,6 +159,7 @@
handleMethod: function(link) {
var href = link.attr('href'),
method = link.data('method'),
+ target = link.attr('target'),
csrf_token = $('meta[name=csrf-token]').attr('content'),
csrf_param = $('meta[name=csrf-param]').attr('content'),
form = $('<form method="post" action="' + href + '"></form>'),
@@ -168,6 +169,8 @@
metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
}
+ if (target) { form.attr('target', target); }
+
form.hide().append(metadata_input).appendTo('body');
form.submit();
},
View
23 test/public/test/data-method.js
@@ -1,11 +1,15 @@
(function(){
-module('data-method');
+module('data-method', {
+ setup: function() {
+ $('#qunit-fixture').append($('<a />', {
+ href: '/echo', 'data-method': 'delete', text: 'destroy!'
+ }));
+ }
+});
-function submit(fn) {
- $('#qunit-fixture').
- append($('<a />', { href: '/echo', 'data-method': 'delete', text: 'destroy!' }))
- .find('a')
+function submit(fn, options) {
+ $('#qunit-fixture').find('a')
.bind('iframe:loaded', function(e, data) {
fn(data);
start();
@@ -31,4 +35,11 @@ asyncTest('link with "data-method" and CSRF', 1, function() {
});
});
-})();
+asyncTest('link "target" should be carried over to generated form', 1, function() {
+ $('a[data-method]').attr('target', 'super-special-frame');
+ submit(function(data) {
+ equal(data.params._target, 'super-special-frame');
+ });
+});
+
+})();
View
5 test/public/test/settings.js
@@ -26,10 +26,11 @@ $(document).bind('submit', function(e) {
if (!e.isDefaultPrevented()) {
var form = $(e.target), action = form.attr('action'),
name = 'form-frame' + jQuery.guid++,
- iframe = $('<iframe name="' + name + '" />');
+ iframe = $('<iframe name="' + name + '" />'),
+ target_input = '<input name="_target" value="' + form.attr('target') + '" type="hidden" />';
if (action.indexOf('iframe') < 0) form.attr('action', action + '?iframe=true')
- form.attr('target', name);
+ form.attr('target', name).append(target_input);
$('#qunit-fixture').append(iframe);
$.event.trigger('iframe:loading', form);
}

0 comments on commit 888be8a

Please sign in to comment.
Something went wrong with that request. Please try again.