Bug Tracker

Ticket #2868 (new enhancement)

Opened 4 months ago

Last modified 2 months ago

Make $.param recursive

Reported by: the_undefined Assigned to: anonymous
Type: enhancement Priority: major
Milestone: 1.2.4 Component: ajax
Version: 1.2.3 Keywords: param recursive http serialize
Cc: the_undefined Needs: Review

Description

I think it would be great if the $.param function was recursive. The code for this could look similar to that (this is what I use right now):

$.httpSerialize = function(items, parentName) {
	var serializedItems = [], serialize = arguments.callee, encodeItem =  function(key, value) {
		if (value === null || typeof value == 'undefined') return value;
		if (value.constructor == Array) {return serialize(value, key);}
		return (value.constructor == Object)
			? serialize(value, key)
			: (value === true || value === false)
				? key+"="+new Number(value)
				: key+"="+encodeURIComponent(value);
	};
	if (items.constructor == Array)  {
		parentName = parentName || 'item';
		for (var i = 0; i < items.length; i++) {
			var key = parentName+'['+i+']', value = items[i];
			serializedItems.push(encodeItem(key, value));
		}
	} else {
		parentName = parentName || '';
		for (var key in items) {
			var value = items[key];
			if (parentName) {
				serializedItems.push(encodeItem(parentName+'['+encodeURIComponent(key)+']', value));
			} else {
				serializedItems.push(encodeItem(encodeURIComponent(key), value));
			}
		}
	}
	return serializedItems.join("&");
};

If there is interest I can provide a patch with a refactored and cleaned up version of the above. I pretty much find myself needing a recursive param version every time I work on a complex app.

Attachments

Change History

Changed 4 months ago by the7erm

Oddly enough I did this tonight as well. It's nice to know that I'm not the only person out there who wants this. http://dev.jquery.com/ticket/2871 http://the-erm.com/~erm/player2/mda.php // here's the demo.

Changed 4 months ago by flesler

  • owner deleted
  • component changed from core to ajax

Changed 2 months ago by the_undefined

Hey, is there no interest in this or is just nobody having the time? I can come up with a patch for this if thats what needs to be done ; )

Note: See TracTickets for help on using tickets.