Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-params handling to handleMethod #395

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,43 @@
var href = rails.href(link),
method = link.data('method'),
target = link.attr('target'),
params = link.data('params'),
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" />';
dataInputs = '<input name="_method" value="' + method + '" type="hidden" />';

if (csrfParam !== undefined && csrfToken !== undefined) {
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
dataInputs += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}

if (params) {

function buildParamsInputs(key, value) {
switch (typeof value) {
case 'array':
var result = [];
for (var i = 0, l = value.length; i < l; i++) {
result.push( buildParamsInputs(key === null ? i : key + '[' + i + ']', value[i]) );
}
return result.join('');
case 'object':
var result = [];
for (var i in value) {
result.push( buildParamsInputs(key === null ? i : key + '[' + i + ']', value[i]) );
}
return result.join('');
default:
return '<input name="' + key + '" value="' + String(value).replace(/"/g, '&quot;') + '" type="hidden" />';
}
}

dataInputs += buildParamsInputs(null, params);
}

if (target) { form.attr('target', target); }

form.hide().append(metadataInput).appendTo('body');
form.hide().append(dataInputs).appendTo('body');
form.submit();
},

Expand Down
18 changes: 18 additions & 0 deletions test/public/test/data-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ asyncTest('link with "data-method" set to "delete"', 3, function() {
});
});

asyncTest('link with "data-method" set to "post" and "data-params"', 4, function() {
var value1 = 0,
value2 = '\'quoted"/>&<\'value"',
value3 = {foo: {bar: {baz: value2}}},
params = {
data1: value1,
data2: value2,
data3: value3
};
$('a[data-method]').attr({'data-method': 'post', 'data-params': JSON.stringify(params)});
submit(function(data) {
equal(data.REQUEST_METHOD, 'POST');
equal(data.params.data1, value1, 'params should have key data1 with right value');
equal(data.params.data2, value2, 'params should have key data2 with right value');
propEqual(data.params.data3, {foo: {bar: {baz: value2}}}, 'params should have key data3 with right value');
});
});

asyncTest('link with "data-method" and CSRF', 1, function() {
$('#qunit-fixture')
.append('<meta name="csrf-param" content="authenticity_token"/>')
Expand Down
23 changes: 23 additions & 0 deletions test/public/test/data-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ asyncTest('clicking on a link with data-remote attribute', 5, function() {
.trigger('click');
});

asyncTest('clicking on a link with data-remote attribute and data-params as JSON', 6, function() {
var value1 = 0,
value2 = '\'quoted"/>&<\'value"',
value3 = {foo: {bar: {baz: value2}}},
params = {
data1: value1,
data2: value2,
data3: value3
};
$('a[data-remote]')
.attr('data-params', JSON.stringify(params))
.bind('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success');
App.assertRequestPath(data, '/echo');
equal(data.params.data1, value1, 'params should have key data1 with right value');
equal(data.params.data2, value2, 'params should have key data2 with right value');
propEqual(data.params.data3, {foo: {bar: {baz: value2}}}, 'params should have key data3 with right value');
App.assertGetRequest(data);
})
.bind('ajax:complete', function() { start() })
.trigger('click');
});

asyncTest('clicking on a button with data-remote attribute', 5, function() {
$('button[data-remote]')
.bind('ajax:success', function(e, data, status, xhr) {
Expand Down
3 changes: 1 addition & 2 deletions test/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ def jquery_versions
content_type 'application/json'
data.to_json
elsif params[:iframe]
payload = data.to_json.gsub('<', '&lt;').gsub('>', '&gt;')
<<-HTML
<script>
if (window.top && window.top !== window)
window.top.jQuery.event.trigger('iframe:loaded', #{payload})
window.top.jQuery.event.trigger('iframe:loaded', #{data.to_json})
</script>
<p>You shouldn't be seeing this. <a href="#{request.env['HTTP_REFERER']}">Go back</a></p>
HTML
Expand Down