jquery-ujs
jquery-ujs copied to clipboard
Add data-params handling to handleMethod
This commit adds the functionality to fetch the data-params attribute from the link and add fields to the form that express the values from the data-param attributes.
This is useful for POST-requests via links that should post parameters instead of sending them in the URL.
:+1:
I've got similar behavior in an extension I've written (except mine handles params of type object as well by creating rails-style bracket names, eg <input name="foo[bar][baz]" value="quux" type="hidden"> for a param foo with value {bar: {baz: 'quux'}}).
one suggestion: you probably want to replace any " characters in params[key] with " so that the generated string is parsed by the browser correctly.
this needs to be pulled ASAP!!!!
This can't be pulled in as-is. There's no such thing as a params attribut on a link tag, so using this would require the use of invalid HTML. Instead, it should be looking for the data-params attribute, which would be accessed via link.data('params').
Also, I don't get line 200. The params variable would be populated with the value of data-params, which would be a string, but then you're looping through it like it's an array or object.
using attr instead of data is probably a simple mistake. and his use case probably requires data('params') to be set programmatically with an object.
if @phillipp doesn't get around to updating his pull request in a couple days then I'll submit one that addresses these issues.
Yes, use of attr() was a mistake while copying my path over to another repo. This is fixed by the last commit. As passing an object into the data-params makes the most sense for me, the method is looping over the object.
Hey @phillipp, could you squash your two commits down into one and update this PR? We also need to get a test added to make sure this is working as intended. If you could do that, I can pull this in right away. otherwise, I'll have to write one when I get some time.
Hi @JangoSteve, thanks for picking this up again. I suqashed the two commits, but I just have a few hours left before some travel and I can't deal with the testing right now. Sorry.
Thanks. I'll have to write a test when I get some time then. The one thing I'm concerned about, is that this only allows for data-params to be added as an actual jQuery data object on the link via JS. In fact, if someone had something like
<a href="/somewhere" data-remote=true data-method="post" data-params="my_value=hello">Click</a>
This would append 14 hidden fields to be appended to the form like this:
<input name="0" value="m" type="hidden" />
<input name="1" value="y" type="hidden" />
...
And so on. Given that data-params allows a string or object elsewhere in jquery-ujs, I think it'd be reasonable for people to expect this to be consistent.
I'm wondering if we should check to see if it's a string first, and if so, append it to the form's action.