jquery-address
jquery-address copied to clipboard
support objects
had made some changes to support objects for this version: http://github.com/asual/jquery-address/blob/6cbd64b2f7784adc746f645980bc3121c8b0ab66/src/jquery.address.js
now it doesn't work but it could help you to support objects i changed only this 2 functions and added one (the deparam function is from a other jquery plugin with a small change). if you don't like it, close it :)
_parameter = function(name, value) {
value = _queryString(value);
if (value) {
params = value.split('&');
var r = [];
for (i = 0; i < params.length; i++) {
var p = params[i].split('=');
if (p[0] == name) {
var temp=$.address.decode(p.slice(1).join('='));
if(temp.indexOf('=')!=-1){
temp=$.address.deparam(temp);
}
r.push(temp);
//r.push(p.slice(1).join('='));
}
}
if (r.length !== 0) {
return r.length != 1 ? r : r[0];
}
}
},
parameter: function(name, value, append) {
var i, params;
if (value !== undefined) {
if(typeof value=='object'){
value=_encode($.param(value));
}
var names = this.parameterNames();
params = [];
for (i = 0; i < names.length; i++) {
var n = names[i],
v = this.parameter(n);
if (typeof v == 'string') {
v = [v];
}
if (n == name) {
v = (value === null || value === '') ? [] :
(append ? v.concat([value]) : [value]);
}
for (var j = 0; j < v.length; j++) {
params.push(n + '=' + v[j]);
}
}
if ($.inArray(name, names) == -1 && value !== null && value !== '') {
params.push(name + '=' + value);
}
this.queryString(params.join('&'));
return this;
}
return _parameter(name, this.value());
},
deparam: function(params,firstdecodeURIComponent){
var obj={},
params=params || '',
firstdecodeURIComponent=firstdecodeURIComponent || true
;
//coerce_types={'true':!0,'false':!1,'null':null};
if(firstdecodeURIComponent){
params=decodeURIComponent(params);
}
// Iterate over all name=value pairs.
$.each(params.replace(/\+/g,' ').split('&'),function(j,v){
var param=v.split('='),
key=decodeURIComponent(param[0]),
val,
cur=obj,
i=0,
// If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it
// into its component parts.
keys=key.split(']['),
keys_last=keys.length - 1
;
// If the first keys part contains [ and the last ends with ], then []
// are correctly balanced.
if(/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])){
// Remove the trailing ] from the last keys part.
keys[keys_last]=keys[keys_last].replace(/\]$/,'');
// Split first keys part into two parts on the [ and add them back onto
// the beginning of the keys array.
keys=keys.shift().split('[').concat(keys);
keys_last=keys.length-1;
}else{
// Basic 'foo' style key.
keys_last=0;
}
// Are we dealing with a name=value pair, or just a name?
if(param.length===2){
val=decodeURIComponent(param[1]);
// Coerce values.
/*if(coerce){
val=val && !isNaN(val) ? +val // number
: val === 'undefined' ? undefined // undefined
: coerce_types[val] !== undefined ? coerce_types[val] // true, false, null
: val; // string
}*/
if(keys_last){
// Complex key, build deep object structure based on a few rules:
// * The 'cur' pointer starts at the object top-level.
// * [] = array push (n is set to array length), [n] = array if n is
// numeric, otherwise object.
// * If at the last keys part, set the value.
// * For each keys part, if the current level is undefined create an
// object or array based on the type of the next keys part.
// * Move the 'cur' pointer to the next level.
// * Rinse & repeat.
for(;i<=keys_last;i++){
key=(keys[i]==='') ? cur.length : keys[i];
cur=cur[key]=(i<keys_last)
? cur[key] || ( keys[i+1] && isNaN( keys[i+1] ) ? {} : [] ): val;
}
}else{
// Simple key, even simpler rules, since only scalars and shallow
// arrays are allowed.
if($.isArray(obj[key])){
// val is already an array, so push on the next value.
obj[key].push(val);
}else if(obj[key]!==undefined){
// val isn't an array, but since a second value has been specified,
// convert val into an array.
obj[key]=[obj[key],val];
}else{
// val is a scalar.
obj[key]=val;
}
}
}else if(key){
// No value was defined, so set something meaningful.
//obj[key]=(coerce) ? undefined : '';
obj[key]='';
}
});
return obj;
},