#11002 closed feature (patchwelcome)
Add serializeObject method to core
Reported by: | ajpiano | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.7.1 |
Keywords: | 1.8-discuss | Cc: | |
Blocked by: | Blocking: |
Description (last modified by )
It's probably more useful into have a method in core for serializing a form directly to an object, instead of an array of objects with name and value keys. Luckily, Ben Alman has a very small implementation that leverages the existence of serializeArray to give us just such a method.
My experience tells me this is a relatively common use-case: I've used it on a number of projects, and I've directed numerous people toward it as well in the last year or two. Not only is it beneficial when developing single-page apps with Backbone or the like, it's considerably simpler than existing options (in core) for users who need to submit a form via ajax, but also want to augment the data with some properties beforehand.
https://raw.github.com/cowboy/jquery-misc/master/jquery.ba-serializeobject.js
While most plugins should stay just that - plugins - I think that this feature is common enough, lean enough, and low-level enough for us to be able to offer it in core.
Change History (18)
comment:1 Changed 11 years ago by
Keywords: | 1.8-discuss added |
---|
comment:2 Changed 11 years ago by
Description: | modified (diff) |
---|
comment:4 Changed 11 years ago by
Description: | modified (diff) |
---|
+1, Okay okay you convinced me. Somewhat of a side note, do we need a general method to convert arrays to hashes? I haven't looked at the code to see how often we do that.
comment:5 Changed 11 years ago by
Status: | new → open |
---|
comment:6 Changed 11 years ago by
Description: | modified (diff) |
---|
+0, I don't really see this getting used without the presence of some other plugin.
comment:9 Changed 11 years ago by
Keep in mind that the logic in my $.fn.serializeObject
plugin is rather naive, and will only create the most basic objects. See this fiddle to get a better idea of what I'm talking about:
http://jsfiddle.net/cowboy/Qxc72/
If you want to do this "right"—ie. less feature requests than you're currently getting—one of two things probably has to happen:
1) a much more powerful form parser will need to be created, and with it an updated $.param
serializer
2) a $.deparam
compliment to the existing $.param
(surprise, surprise, another plugin of mine)
or, of course, a third alternative:
3) "wontfix"
I'm more than happy to work with you guys to help make this happen, or to at least help explain what's really involved if I haven't made it clear enough.
Let me know!
comment:10 Changed 11 years ago by
@cowboy: I think we're interested in seeing an implementation. It's hard to say whether it will make it into core without first seeing its impact on the code.
comment:11 Changed 11 years ago by
If this is going to get a lot bigger to handle more cases, I'm likely to move my +1 to a -1. However, the effort wouldn't be wasted if the outcome was a solid plugin.
comment:14 Changed 11 years ago by
The duplicate above was mine, so thought I'd suggest on this thread allowing passing a parameter "string", "array" or "object" to the serialize() method (defaulting to "string") rather than adding new methods
comment:15 Changed 11 years ago by
Just use the code from the fiddle above: https://raw.github.com/cowboy/jquery-misc/master/jquery.ba-serializeobject.js
This is too small to really be put into a plugin. If it turns into some larger function, then discuss taking it out and putting it back into a plugin. But, honestly, I don't really see that happening.
comment:16 Changed 11 years ago by
@cowboy is saying (and I agree) that the simple implementation in the OP would probably not be robust enough, and we'd get bugs filed against its shortcomings. So before we land it I think we'd need to see what a complete implementation looks like. First we need an owner.
comment:17 Changed 11 years ago by
Resolution: | → patchwelcome |
---|---|
Status: | open → closed |
Still open to an implementation here, with the caveat that we don't want a large amount of code. If it's a lot, it should be a plugin.
comment:18 Changed 11 years ago by
Just as a starting point: http://jsfiddle.net/DxC5J/3/
Checkboxes do not seem to return a useful value.
+1