1. #1
    Ext User violinista's Avatar
    Join Date
    Apr 2007
    Location
    Serbia
    Posts
    293
    Vote Rating
    0
    violinista is on a distinguished road

      0  

    Default Useful way to serialize complex objects and arrays...

    Useful way to serialize complex objects and arrays...


    Hello folks, I found an useful way to serialize objects and applied, with slightly modifications, to our lovely Ext.

    First, to define problem: in some (rare, but real!) cases, you want to pass complex object or multi-dimension arrays to server-side. For example, if you want to pass this object:
    Code:
    {foo:'bar'}
    ...it'll be encoded as:
    Code:
    foo=bar
    But, what if you want to pass object like this:
    Code:
    {foo:['bar1','bar2']}
    To get this object properly, it must be encoded like this:
    Code:
    foo[]=bar&foo[]=bar2
    That is the point, and I found here useful method which works for me.
    The urlEncode-advanced function in Ext, which works good so far (for me), but - I suppose so - could be much improved and more optimized is:

    PHP Code:
    Ext.apply(Ext,{

    urlEncode:function(arr,current_index) {

        if(!
    arr){
            return 
    "";
        }
        var 
    query ""
        
    if(typeof current_index=='undefined'current_index '';

        if(
    typeof(arr) == 'object') {
            var 
    params = new Array();
            for(
    key in arr) {
                var 
    data arr[key];
                var 
    key_value key;
                if(
    current_index) {
                    
    key_value current_index+"["+key+"]"
                
    }

                if(
    typeof(data) == 'object') {
                    if(
    data.length) { //List
                        
    for(var i=0;i<data.lengthi++) {
                            
    params.push(key_value+"[]="+this.urlEncode(data[i],key_value)); //:RECURSION:
                        
    }
                    } else { 
    //Associative array
                        
    params.push(this.urlEncode(data,key_value)); //:RECURSION:
                    
    }
                } else { 
    //String or Number
                    
    params.push(key_value+"="+encodeURIComponent(data));
                }
            }
            
    query params.join("&");
        } else {
            
    query encodeURIComponent(arr);
        }
        return 
    query;
    } }); 
    As stated in the link before, now you can do something like:
    PHP Code:
    var arr = {
        
    'name':"Violinista",
        
    'year':2007,
        
    'quote':"Hello, World!",
        
    'os':['Windows','Linux','Mac'],
        
    'software':{
            
    'editor':"vi",
            
    'audio':"xmms",
            
    'video':"vlc"
        
    }
    }
    console.log(Ext.urlEncode(arr)); 
    Note that comments and improvements are much appreciated.

    Note: I needed this stuff as I'm writing some king of query-by-example form (which will be presented here when finished!), and that kind of form sends much complicated information to server-side, when reloading datagrid with its parameters. This way, I can add to ds.load all the params I need and it is done on ExtJs standard way.

    Regards
    "It is better to be young, pretty and rich instead old, ugly and poor."
    (c) Alan Ford.

  2. #2
    Sencha Developer
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    482
    Vote Rating
    1
    Wolfgang is on a distinguished road

      0  

    Default


    Hello,

    why do you not use JSON?

    Regards

    Wolfgang

  3. #3
    Ext User violinista's Avatar
    Join Date
    Apr 2007
    Location
    Serbia
    Posts
    293
    Vote Rating
    0
    violinista is on a distinguished road

      0  

    Default


    I don't know the other way to serialize object and send it as JSON via GET or POST request.
    "It is better to be young, pretty and rich instead old, ugly and poor."
    (c) Alan Ford.

  4. #4
    Sencha - Community Support Team JeffHowden's Avatar
    Join Date
    Mar 2007
    Location
    Forest Grove, OR
    Posts
    1,038
    Vote Rating
    1
    JeffHowden is on a distinguished road

      0  

    Default


    I agree, urlEncode and urlDecode are for encoding and decoding valid name/value pairs as you'd find in a URL. That PHP has made special cases for certain characters being a part of the name portion of the pair causing arrays to be generated doesn't necessarily mean it has to be supported in a pure url encoding/decoding method in Ext. Simply, the urlEncode and urlDecode are for working with simple name/value pairs.

    If you want to pass complex data back and forth, use JSON, that's what it's for. Pass an object of any manner of complexity to Ext.util.JSON.encode() and you'll get back a JSON string. If you receive a JSON string from the server, pass it to Ext.util.JSON.decode() and you'll receive back an object of appropriate complexity.
    Jeff Howden
    Ext JS - Support Team Volunteer
    jeff@extjs.com

    Any and all code samples that are authored by me and posted on the Ext forums or website are hereby released into the public domain and I release anyone or entity of liability by using said code samples unless explicitly stated otherwise.

    Opinions are mine and not necessarily endorsed by Ext, LLC. Please do not contact me directly for assistance unless requested by me.

  5. #5
    Ext User violinista's Avatar
    Join Date
    Apr 2007
    Location
    Serbia
    Posts
    293
    Vote Rating
    0
    violinista is on a distinguished road

      0  

    Default


    Ok, suppose I have datagrid with dataset accepting complex object in need to produce WHERE condition properly. With this tool I can do simply:
    ds.load({params:myComplexObject})
    ...and all params will be passed correctly. As simple as that. Note:I have more than 10 arrays of complex parameters to pass to my Db. If someone knows more elegant way to solve this, I will appreciate to see it.
    Greetz, Violinista
    "It is better to be young, pretty and rich instead old, ugly and poor."
    (c) Alan Ford.

  6. #6
    Sencha - Community Support Team JeffHowden's Avatar
    Join Date
    Mar 2007
    Location
    Forest Grove, OR
    Posts
    1,038
    Vote Rating
    1
    JeffHowden is on a distinguished road

      0  

    Default


    I already outlined precisely how to do it. Using JSON, you know server-side when you deserialize the JSON string which portions are objects, which are arrays, and which are simple values. Even better, there are tools available in nearly application server language to decode or deserialize JSON strings meaning you don't need to do any manual coding to convert them.
    Jeff Howden
    Ext JS - Support Team Volunteer
    jeff@extjs.com

    Any and all code samples that are authored by me and posted on the Ext forums or website are hereby released into the public domain and I release anyone or entity of liability by using said code samples unless explicitly stated otherwise.

    Opinions are mine and not necessarily endorsed by Ext, LLC. Please do not contact me directly for assistance unless requested by me.

Thread Participants: 2

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar