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

Conversation

KODerFunk
Copy link
Contributor

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.

My version of forgotten #307 from @phillipp, with escaping and composing hidden inputs from complex objects by @bcm's request.

@stereodenis
Copy link

👍 good feature!

@phillipp
Copy link

👍 would love to see this merged soon ;-)

@ecbypi
Copy link

ecbypi commented Oct 28, 2014

It seems like this is re-building the functionality of a <form> element using data-remote. button_to generates a <form> element, can be used with remote: true and has a :params option for specifying data with hidden inputs.

If it needs to look like a link, that can be achieved through CSS (see bootstrap's btn-link CSS class).

@rafaelfranca
Copy link
Member

Yes. This is doing the same as button_to helper. Could you explain why not using it?

@KODerFunk
Copy link
Contributor Author

@ecbypi, functionality of a <form> element using data-remote for use data-params realized in handleRemote method.
My implementation is reasonable compliant functionality of using data-params with data-method attributed elements without data-remote (non ajax).
For example

<a href="/admin/parsings" data-method="post" data-params='{"kind_id": "1"}'>Start some kind parsing</a>"

@rafaelfranca
Copy link
Member

I don't think we should promote JSON usage on data tags. If you need to pass params in a link use the button_to helper.

@stereodenis
Copy link

@rafaelfranca and how to use button_to helper to pass params without data-remote ?

@rafaelfranca
Copy link
Member

It created hidden inputs inside the form tag.

@KODerFunk
Copy link
Contributor Author

@rafaelfranca, do you think reasonable that parameters are passed in this case:

<a href="/admin/parsings" data-remote="true" data-method="post" data-params='{"kind_id": "1"}'>Start some kind parsing</a>"

but there is no way?

<a href="/admin/parsings" data-method="post" data-params='{"kind_id": "1"}'>Start some kind parsing</a>"

Both constructions are handled by jquery-ujs, but in the second case surprisingly no parameters are passed.

@rafaelfranca
Copy link
Member

O_O. If first is working, there is no reason for the second to not work.

@rafaelfranca rafaelfranca reopened this Oct 29, 2014
@KODerFunk
Copy link
Contributor Author

I add test for first case, demonstrate coorrect work of JSON params.
And now i work on removing unnecessary escapeHTML.
I doubt the necessity of 443de05. In latest FF and Chrome (MacOS) pass all test without escaping. Now will testing in old IE versions (Windows).

@stereodenis
Copy link

@KODerFunk 👍

@stereodenis
Copy link

@rafaelfranca what do you think about this PR?

@rafaelfranca
Copy link
Member

I prefer to defer this decision to @JangoSteve or @lucasmazza?

@JangoSteve
Copy link
Member

Sorry, I had to go back and read through the conversation from #307 to remind myself of the initial intent and discussion.

There are a few issues with this PR as it stands, that lead me to believe that #307 might be closer to the desired implementation:

  • We shouldn't have the buildParamsInputs function defined the way it is within the handleMethod function. If we're going to implement that function, it should be extracted out so it can be reused.

  • That said, I don't necessarily like adding the buildParamsInputs function in the first place, even if it were to be extracted out. If we want to be able to parameterize arrays and hashes, we should use jQuery built-in functionality for that, the same way jQuery does it for $.ajax() functions when you pass the data attribute. For example, see $.param() and $('form').serialize() functions in jQuery. This would help us keep with the element of least surprise.

  • Why are we changing test.server.rb for this pull request? That's a low-level thing that's used by a lot of the tests in the test suite. I'd prefer it to remain unchanged, or if it's really needed for some reason, it should be done in a separate pull-request with an explanation that shows some understanding of why it was like that in the first place.

    I say this, only because I honestly don't remember off the top of my head why it is the way it is, but I can tell you that a lot of thought and effort went into writing it the way it is, back when we did it; it was just a long time ago. Then again, I could be wrong, and maybe there's no good reason for it to be that way.

    But it may have had something to do with getting the tests to pass in all browsers. Did you try running the updated test suite, for example, in Opera and older versions of IE? That's where a lot of the weird code like that in the test suite came from.

  • I don't fully understand the default handler in your case statement. I think one of the main use-cases of this would be my comment here data-params="my_value=hello", like you can with remote links and other remote elements.

    As far as I can tell, your implementation does not allow any sort of setting params using the HTML data-params="..." attribute, because if you have an element with this, this is going to pass null in as key, and your string in as the value, which will then produce a hidden field with name="null" and <input name="null" value="my_value=hello&other_value=bye"" type="hidden" />, which isn't going to do what you expect it to.

    Of course, as the test cases show, this implementation only supports directly setting $("#my-link").data("params", {some: "object or array"}) programmatically via JS.

@KODerFunk
Copy link
Contributor Author

@JangoSteve, I see. I agree with your arguments. Work on this at the end of the week.

@JangoSteve
Copy link
Member

@KODerFunk Awesome, sounds good! Thanks!

@davetoxa
Copy link

👍 For this PR

@pgolm
Copy link

pgolm commented Feb 5, 2016

👍

@nmfzone
Copy link

nmfzone commented Nov 1, 2016

Is there no progress on this? We (especially me) need this feature.

@chewi
Copy link

chewi commented Feb 12, 2019

I was rather surprised to find that this doesn't work. 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants