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

Serialize multi-select values as an array #101

Open
wants to merge 8 commits into
base: minor
Choose a base branch
from

Conversation

patrickbajao
Copy link

Hi,

I have a multi-select in one of my forms. I started using Backbone.Syphon to serialize that form and I realized that it wasn't returning the values I am expecting. Before this change, it's returning the values of a multi-select as a nested array:

[
  [
    "bar1",
    "bar2"
  ]
]

When submitting this format of serialized data to my backend (using Rails) via AJAX, the URL encoded value will be foo%5B0%5D%5B%5D=bar1&foo%5B0%5D%5B%5D=bar2. That looks weird and that's not what I am expecting.

My expected output is:

[
  "bar1",
  "bar2"
]

Which will be foo%5B%5D=bar1&foo%5B%5D=bar2 when URL encoded.

Regarding the fix found in this pull request, I updated the assignKeyValue method to push each value into the array instead of pushing the array into an array to avoid the nested array output. I also added a spec for that and hopefully that helps in explaining my case further. All tests are still passing with these changes.

If I'm still missing something or if I need to change something else, please let me know. :)

Thanks,
Patrick

@rhubarbselleven
Copy link
Contributor

Thanks for the great work @patrickbajao

Could you discuss the cross over of this one with #34?

@patrickbajao
Copy link
Author

You're welcome @rhubarbselleven! :)

I'm not entirely sure what's the connection of my issue with that one. I think it's a different issue because I don't really have nested attributes. Though looks like my changes in this pull request might conflict with any changes that needs to be done on that issue. It seems that the fix for that requires modifying assignKeyValue which is what I also modified here. Is that what you are pertaining to?

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.

None yet

3 participants