The module mainly handles the query part of the URL, and when we send a GET request, the parameters are placed directly in the URL, and often the requirement is to convert an object into a query string and put it into the URL to send a GET request. The Io-query module provides two ways to handle query:
- Objecttoquery converts an object into a query string
- Querytoobject Convert query string to object
Converting from an object to query is the process of stitching, and the fields and values of an object need to be coded
Objecttoquery:functionObjecttoquery (/*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" varENC = encodeuricomponent, pairs = []; for(varNameinchmap) { varValue =Map[name]; //backstop is an empty object that is designed to prevent fields such as the ToString inherited from being processed if(Value! =Backstop[name]) { varassign = ENC (name) + "="; if(Lang.isarray (value)) {//arrays are encoded into each array of elements: Multi=thud&multi=thonk for(vari = 0, L = value.length; I < L; ++i) {Pairs.push (Assign+enc (value[i])); } }Else{Pairs.push (assign+Enc (value)); } } } returnPairs.join ("&");//String}
Query to object is the process of string splitting, both parameters and parameter values need to be decoded using decodeuricomponent
Querytoobject:functionQuerytoobject (/*String*/str) { //Summary: //Create An object representing a de-serialized query section of a //URL. Query keys with multiple values is returned in an array. // //Example: //This string: // // | "foo=bar&foo=baz&thinger=%20spaces%20=blah&zonk=blarg&" // //results in this object structure: // // | { // | Foo: ["Bar", "Baz"], // | Thinger: "Spaces =blah", // | Zonk: "Blarg" // | } // //Note that spaces and other urlencoded entities is correctly //handled. //String Segmentation varDec = decodeuricomponent, QP = Str.split ("&"), ret ={}, name, Val; for(vari = 0, L = qp.length, item; I < L; ++i) {Item=Qp[i]; if(item.length) {vars = item.indexof ("="); if(S < 0) {//Null valueName =Dec (item); Val= ""; }Else{ //split "A=b" in the form of name A,val bname = Dec (Item.slice (0, s)); Val= Dec (Item.slice (s + 1)); } //a name that corresponds to more than one value is converted to an array if(typeofRet[name] = = "string") {//inline ' d type checkRet[name] =[Ret[name]]; } if(Lang.isarray (Ret[name])) {Ret[name].push (val); }Else{Ret[name]=Val; } } } returnRet//Object}
Dojo/io-query Source Code Analysis