1. #1
    Sencha User Lobos's Avatar
    Join Date
    Oct 2007
    Location
    Sao Paulo, Brazil
    Posts
    461
    Vote Rating
    -1
    Lobos is an unknown quantity at this point

      0  

    Default [SOLVED]serialize object

    [SOLVED]serialize object


    HI there,

    I have a store and I would like to serialize the data for export, is there such a function as this for the store? I know I can do something like this:

    store.data.each(function() {
    //do something with this.data
    });

    but I was wondering if there was something like

    Ext.serialize(store.data);

    That would put the object as JSON into a string for export...

    here is some info on this so I might just try one of these solutions (if there is nothing in Extjs that can)

    crockford's stringify:
    http://www.json.org/json2.js

    and something else here
    http://blog.stchur.com/2007/04/06/se...in-javascript/

    -Lobos

  2. #2
    Sencha User Lobos's Avatar
    Join Date
    Oct 2007
    Location
    Sao Paulo, Brazil
    Posts
    461
    Vote Rating
    -1
    Lobos is an unknown quantity at this point

      0  

    Default


    I ended up using this function from:

    http://blog.stchur.com/2007/04/06/se...in-javascript/

    PHP Code:
    function serialize(_obj)
    {
       
    // Let Gecko browsers do this the easy way
       
    if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
       {
          return 
    _obj.toSource();
       }

       
    // Other browsers must do it the hard way
       
    switch (typeof _obj)
       {
          
    // numbers, booleans, and functions are trivial:
          // just return the object itself since its default .toString()
          // gives us exactly what we want
          
    case 'number':
          case 
    'boolean':
          case 
    'function':
             return 
    _obj;
             break;

          
    // for JSON format, strings need to be wrapped in double-quotes
          
    case 'string':
             return 
    '\'' _obj '\'';
             break;

          case 
    'object':
             var 
    str;
             if (
    _obj.constructor === Array || typeof _obj.callee !== 'undefined')
             {
                
    str '[';
                var 
    ilen _obj.length;
                for (
    0len-1i++) { str += serialize(_obj[i]) + ','; }
                
    str += serialize(_obj[i]) + ']';
             }
             else
             {
                
    str '{';
                var 
    key;
                for (
    key in _obj) { str += key ':' serialize(_obj[key]) + ','; }
                
    str str.replace(/\,$/, '') + '}';
             }
             return 
    str;
             break;

          default:
             return 
    'UNKNOWN';
             break;
       }

    I still needed to use the each a little bit... but the string is ready to be exported and decoded on the server side.

    PHP Code:
    var count 0;
    store.data.each(function() {
        
    expData[count++] = this.data;
    });
                            
    alert(serialize(expData)); 

  3. #3
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,087
    Vote Rating
    113
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    Try Ext.encode(myData)
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  4. #4
    Sencha User Lobos's Avatar
    Join Date
    Oct 2007
    Location
    Sao Paulo, Brazil
    Posts
    461
    Vote Rating
    -1
    Lobos is an unknown quantity at this point

      0  

    Default


    LOL yep that's it... hahahahha I did try this, but before I did the each() so I was getting the "too much recursion error", but now it's working perfectly sooo, now it;s just this:

    var count = 0;
    store.data.each(function() {
    expData[count++] = this.data;
    });

    alert(Ext.encode(expData));

    that's better thanks for the advice!

    -Lobos

  5. #5
    Sencha User
    Join Date
    Dec 2010
    Posts
    5
    Vote Rating
    0
    hmezamx is on a distinguished road

      0  

    Default Serializing a grid columns model

    Serializing a grid columns model


    I need to serialize the grid's column model. The premise is that I need to be able to store the grid configuration to the database for reuse and I'm trying to accomplish this by serializing the column model and consume it back.

    I forgot to mention that I tried stringifying the object but the column model apparently has circular structures

    is this possible or is there a better approach.


    ~Thanks

  6. #6
    Sencha User
    Join Date
    Nov 2010
    Posts
    47
    Vote Rating
    0
    anshu_gupta is on a distinguished road

      0  

    Default


    I have a gridpanel. It has a checkbox colums model. i want to select the rows through checkboxes and send those records to server.
    I get the data in Firebug but i get a too much recursion error. I am using getSelections() method. i want to know how can i get the id's of the all the elements in the array and post to server. Ext.encode() is not working.

  7. #7
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    You can't Ext.encode records because they contain a store property that in turn contains the records again.

    You'll manually have to loop over the records and collect the relevant data in an array.

  8. #8
    Sencha User
    Join Date
    Nov 2010
    Posts
    47
    Vote Rating
    0
    anshu_gupta is on a distinguished road

      0  

    Default


    Thanks.
    But how to loop over in Ext-Js and how to add elements in an array. a lilttle example would be a great help.
    I saw your post regarding too much recursion error but that did not help much.

  9. #9
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Code:
    var data = [];
    Ext.each(grid.getSelectionModel().getSelections(), function(rec){
      data.push(rec.get('id'));
    });

  10. #10
    Sencha User
    Join Date
    Nov 2010
    Posts
    47
    Vote Rating
    0
    anshu_gupta is on a distinguished road

      0  

    Default


    Thanks. I tried it but its still not working. Can you tell me how to send an array object to server


    Quote Originally Posted by Condor View Post
    Code:
    var data = [];
    Ext.each(grid.getSelectionModel().getSelections(), function(rec){
      data.push(rec.get('id'));
    });