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 
recurse

The 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:
  
Hi,

Been stuck on this for the past 6 hours, so hope you guys can help, I
have an array of 'images' I want to supply to the  AJax.Request and Im
building a hash so I can just convert the whole hash to a query
string.  Each image should have two keys, an id and a src so this is
what I want to build.

            var h = $H({
                somekey: 1
                'images[]': [ { id: 0, src: 'http://' }, { id: 1, src
'http://' } ]
            });

However I need to build the pictures by grabbbing the element by
classname, heres what I have currently

            var h = $H({
                listingTitle: $F( 'listingTitle' ), listingUserID:
$F( 'listingUserID' ),
            });

            var pictures = $
( 'picturesHolder' ).getElementsByClassName( 'smallPicture' );
            var a = [];
            pictures.each( function(item) {
                var moo = $H( { uid: item.id, src: item.src } )
                a.push( moo );
            } );

My question is how do I get my array a into my hash?  Ive tried

h.merge( { 'userimages[]': a } );

Which does not work.  I need the [] in the keyname so that PHP
understands its an array when the Request is posted.  Am I going to
have to do things the hard way and convert the hash to a string then
run through the pictures concatenating the string?


    
  
    




  

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to