extjsソース分析-009(Ext.urlDecode/urlEnccode)
1751 ワード
// json url
// e.g. Ext.urlEncode({foo: 1, bar: 2}); would return "foo=1&bar=2"
urlEncode: function(o, pre){//o json ,pre
var undef, buf = [], key, e = encodeURIComponent;
for(key in o){
undef = !Ext.isDefined(o[key]);
Ext.each(undef ? key : o[key], function(val, i){
buf.push("&", e(key), "=", (val != key || !undef) ? e(val) : "");
});
}
if(!pre){
buf.shift();
pre = "";
}
return pre + buf.join('');
},
// URL json
//Ext.urlDecode("foo=1&bar=2"); // returns {foo: "1", bar: "2"}
//Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false); // returns {foo: "1", bar: ["2", "3", "4"]}
urlDecode : function(string, overwrite){//string URL ,overwrite ,false ,
var obj = {},
pairs = string.split('&'),
d = decodeURIComponent,
name,
value;
Ext.each(pairs, function(pair) {
pair = pair.split('=');
name = d(pair[0]);
value = d(pair[1]);
obj[name] = overwrite || !obj[name] ? value :
[].concat(obj[name]).concat(value);
});
return obj;
}