-
21 Jan 2008 8:44 AM #1
[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
-
21 Jan 2008 9:27 AM #2
I ended up using this function from:
http://blog.stchur.com/2007/04/06/se...in-javascript/
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: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 i, len = _obj.length;
for (i = 0; i < len-1; i++) { 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;
}
}
PHP Code:var count = 0;
store.data.each(function() {
expData[count++] = this.data;
});
alert(serialize(expData));
-
21 Jan 2008 10:20 AM #3
Try Ext.encode(myData)
Aaron Conran
@aconran
Sencha Architect Development Team
-
21 Jan 2008 10:56 AM #4
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
-
22 Dec 2010 12:50 PM #5
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
-
24 Dec 2010 9:03 PM #6
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.
-
24 Dec 2010 11:19 PM #7Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
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.
-
25 Dec 2010 12:00 AM #8
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.
-
25 Dec 2010 2:03 AM #9Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
Code:var data = []; Ext.each(grid.getSelectionModel().getSelections(), function(rec){ data.push(rec.get('id')); });
-
26 Dec 2010 7:53 PM #10



Reply With Quote