Ticket #5906 (closed enhancement: wontfix)

Opened 9 months ago

Last modified 5 months ago

[patch][cla]Represent hashes in dojo.objectToQuery

Reported by: guest Owned by: jburke
Priority: normal Milestone: 1.2
Component: Core Version: 1.0
Severity: normal Keywords: xhr, ajax, post, object,
Cc:

Description (last modified by jburke) (diff)

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

Changed 9 months ago by peller

  • owner changed from anonymous to jburke
  • summary changed from problems with dojo.objectToQuery to [patch][cla]Represent hashes in dojo.objectToQuery
  • type changed from defect to enhancement
  • milestone deleted

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.

Changed 9 months ago by dylan

  • milestone set to 1.1

Changed 9 months ago by jburke

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.

Changed 9 months ago by peller

  • milestone changed from 1.1 to 1.2

Changed 5 months ago by jburke

  • status changed from new to closed
  • resolution set to wontfix
  • description modified (diff)

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.