|
Ok, I couldn't stand it any longer :) It really isn't that hard to write a recursive function so I don't know what all of the fuss is about.. Here is my rewrite (from scratch) of the Hash.toQueryString function. It will *properly* encode any conglomeration of hashes, arrays and other values you throw at it (as long as it can sensibly be converted to a string). It uses brackets and array indexes that will give you the closest translation to PHP variables as you can get, I don't know about other server side languages as I don't regularly use them. The $H().toQueryString method uses this function so there is no need to overwrite it, just past the code below into any js file that is included after prototype. Test it out here: http://colin.mollenhour.com/posttest.php Colin -------------------------------------------------------- Hash.toQueryString = function(obj){ var pairs = $H(obj).findAll(function(pair){ return pair.value !== undefined; }); if(!pairs) return ''; return pairs.collect(function(pair,index){ return Hash._toQueryString(pair.key, pair.value); }).findAll(function(arg){ return !!arg; }).join('&'); }; Hash._toQueryString = function(key,value){ if(value === null) value = ''; if(typeof value != 'object'){ return key+'='+value.toString(); } if(value.constructor == Array){ var items = value.findAll(function(arg){ return arg !== undefined; }); if(!items.length) return Hash._toQueryString(key, ''); var result = items.collect(function(val,index){ return Hash._toQueryString(key+'['+index+']',val); }); }else{ var pairs = $H(value).findAll(function(pair){ return pair.value !== undefined; }); if(!pairs.length) return Hash._toQueryString(key, ''); var result = pairs.collect(function(pair,index){ return Hash._toQueryString(key+'['+pair.key+']',pair.value); }); } return result.join('&'); }; ----------------------------------------------------------- Colin Mollenhour wrote: I'm not 100% sure if I understand what you are trying to do, but I think the problem is that A) toQueryString has a bug, B) toQueryString doesn't recurseThe bug is that it doesn't add the [] after array names.. doh.. For now you'll just have to build it manually (PITA) or submit a patch but good luck with the latter having any effect if you aren't a member of the Prototype Core team. There is a discussion about this here: http://groups.google.com/group/prototype-core/browse_frm/thread/140fdf88eed057d and it appears toQueryString is soon to be rewritten so I wouldn't bother with a patch, just chime in and let them know you'd like to see support for all standard platforms (e.g. fix lack of []) and you'd like to see full recursion or at least a few levels. Good luck, Colin [EMAIL PROTECTED] wrote: --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~--- |
- [Rails-spinoffs] Re: Adding an array to a hash Colin Mollenhour
- [Rails-spinoffs] Re: Adding an array to a hash [EMAIL PROTECTED]
