Skip to content

Instantly share code, notes, and snippets.

@blowery
Created September 2, 2009 18:23
Show Gist options
  • Save blowery/179877 to your computer and use it in GitHub Desktop.
Save blowery/179877 to your computer and use it in GitHub Desktop.
dojo.objectToQuery = function(/*Object*/ map){
// summary:
// takes a name/value mapping object and returns a string representing
// a URL-encoded version of that object.
// example:
// this object:
//
// | {
// | blah: "blah",
// | multi: [
// | "thud",
// | "thonk"
// | ]
// | };
//
// yields the following query string:
//
// | "blah=blah&multi=thud&multi=thonk"
// FIXME: need to implement encodeAscii!!
var enc = encodeURIComponent;
var pairs = [];
var backstop = {};
for(var name in map){
var value = map[name];
if(value != backstop[name]){
var assign = enc(name) + "=";
if(_d.isArray(value)){
for(var i=0; i < value.length; i++){
pairs.push(assign + enc(value[i]));
}
}else{
pairs.push(assign + enc(value));
}
}
}
return pairs.sort().join("&"); // String
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment