violinista
13 Jul 2007, 6:20 AM
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:
{foo:'bar'}...it'll be encoded as:
foo=barBut, what if you want to pass object like this:
{foo:['bar1','bar2']}To get this object properly, it must be encoded like this:
foo[]=bar&foo[]=bar2That is the point, and I found here (http://www.openjs.com/scripts/data/ued_url_encoded_data/) 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:
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.length; i++) {
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:
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 :)
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:
{foo:'bar'}...it'll be encoded as:
foo=barBut, what if you want to pass object like this:
{foo:['bar1','bar2']}To get this object properly, it must be encoded like this:
foo[]=bar&foo[]=bar2That is the point, and I found here (http://www.openjs.com/scripts/data/ued_url_encoded_data/) 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:
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.length; i++) {
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:
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 :)