Bug Tracker

Modify

Ticket #11002 (closed feature: patchwelcome)

Opened 18 months ago

Last modified 10 months ago

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:
Blocking: Blocked by:

Description (last modified by rwaldron) (diff)

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

comment:1 Changed 18 months ago by ajpiano

  • Keywords 1.8-discuss added

comment:2 Changed 17 months ago by jzaefferer

  • Description modified (diff)

+1

comment:3 Changed 17 months ago by jaubourg

+0

comment:4 Changed 17 months ago by dmethvin

  • 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 17 months ago by dmethvin

  • Status changed from new to open

comment:6 Changed 17 months ago by timmywil

  • Description modified (diff)

+0, I don't really see this getting used without the presence of some other plugin.

comment:7 Changed 17 months ago by mikesherov

+1

comment:8 Changed 17 months ago by rwaldron

  • Description modified (diff)

+1

comment:9 Changed 17 months ago by cowboy

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 17 months ago by timmywil

@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 17 months ago by dmethvin

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:12 Changed 16 months ago by SineSwiper

+10. The other two serialize methods are useless to me.

comment:13 Changed 16 months ago by dmethvin

#11312 is a duplicate of this ticket.

comment:14 Changed 16 months ago by wheresrhys@…

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 14 months ago by SineSwiper

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 12 months ago by dmethvin

@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 months ago by dmethvin

  • Status changed from open to closed
  • Resolution set to patchwelcome

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 10 months ago by dmethvin

Just as a starting point:  http://jsfiddle.net/DxC5J/3/

Checkboxes do not seem to return a useful value.

Last edited 10 months ago by dmethvin (previous) (diff)

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.