Opened 13 years ago

Closed 12 years ago

Last modified 11 years ago

#5906 closed enhancement (wontfix)

[patch][cla]Represent hashes in dojo.objectToQuery — at Version 5

Reported by: guest Owned by: James Burke
Priority: high Milestone: future
Component: Core Version: 1.0
Keywords: xhr, ajax, post, object, Cc: [email protected]
Blocked By: Blocking:

Description (last modified by James Burke)

In order to be able to post AJAX queries like this:

eID=tx_t3dojo_model&tx_t3dojo_type=t3dojo_grid&tx_t3dojo[t3dojo_query_connector]=test_connector&tx_t3dojo[command]=info

a javascript parameter with sub objects needs to be converted correctly

var param =  {
              eID: this.eID,
              tx_t3dojo: {
                      t3dojo_query_connector: this.T3DojoQueryConnector
              }
        };

The change below would do the trick:

dojo.objectToQuery = function(/*Object*/ map){


        var ec = encodeURIComponent;
        var ret = "";
        var backstop = {};
        for(var x in map){
            if(map[x] != backstop[x]){
                if(_d.isArray(map[x])){
                    for(var y=0; y<map[x].length; y++){
                        ret += ec(x) + "=" + ec(map[x][y]) + "&";
                    }
                } /*change achim gerber */ else if(_d.isObject(map[x])){
                    for(var y in map[x]){
                        ret += ec(x) + "[" + ec(y) + "]" + "=" + ec(map[x][y]) + "&";
                    }
                } /* end change achim gerber */else{
                    ret += ec(x) + "=" + ec(map[x]) + "&";
                }
            }
        }
        if(ret.length && ret.charAt(ret.length-1) == "&"){
            ret = ret.substr(0, ret.length-1);
        }
        return ret; // String
    }

Greetings - Achim

Change History (5)

comment:1 Changed 13 years ago by Adam Peller

Milestone: 1.0.3
Owner: changed from anonymous to James Burke
Summary: problems with dojo.objectToQuery[patch][cla]Represent hashes in dojo.objectToQuery
Type: defectenhancement

changed summary to be more descriptive (did I get it right?) wouldn't queryToObject need to support the inverse mapping? does this follow the url query convention of any particular software, or is this something new?

it would be great if you could submit patches as diffs in the future. unit tests are a plus, too.

comment:2 Changed 13 years ago by dylan

Milestone: 1.1

comment:3 Changed 13 years ago by James Burke

I can apply this patch, but favor changing the serialization so that instead of doing this:

ob[propName1]=value1&obj[propName2]=value2

it will be doing this:

ob["propName1"]=value1&obj["propName2"]=value2

Note the inclusion of double quotes inside the brackets. Actually, the brackets and double quotes will be URL encoded to %5B/%5D and %22 respectively if you look at the raw HTTP request.

In the absence of a standard for serializing object properties to URL parameters (or did I miss something?), I'm favoring using the JSON/JavaScript approach of using double quotes around the property name.

Hmm, actually, come to think of it, I'm not sure we should be doing this at all: an equally valid escaping could be using straight JSON for the object:

obj={"propName1":"value1","propName2":"value2"}

Of course the obj value above would be URL encoded. This has the added value of supporting nested objects.

Can anyone provide info on any standards in this area? I'm reticent to code something unless we have a solid precedent. What do other JS libraries do, or do they even try?

I will likely close this as wontfix unless we have solid evidence of a generally accepted standard.

comment:4 Changed 13 years ago by Adam Peller

Milestone: 1.11.2

comment:5 Changed 12 years ago by James Burke

Description: modified (diff)
Resolution: wontfix
Status: newclosed

No further comments on any accepted standards for object to URL parameter conversion. So closing as wontfix. Feel free to re-open if there is a generally accepted standard.

Note: See TracTickets for help on using tickets.