Dojo/io-query Source Code Analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.