0

i have a url

http://www.example.com/

and i want to pass an array to $.bbq.pushState

var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

how can i pass this array by $.bbq.pushState

please help ........................

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

4 Answers4

0

This is how I did in BBQ v1.2.1, and it worked fine for me.

Pushing the state:

var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

$.bbq.pushState({ 'myCars': myCars });

Getting the state in the hashchange event:

$(window).on('hashchange', function (e) {
   var myCars2 = e.getState('myCars');
}
Anttu
  • 1,076
  • 1
  • 10
  • 21
0
var checkedBoxes = {};   // new object outside of function                                     
function toggleLayer(layer) { // layer is your key variable
    var val = $('#'+layer).prop('checked');  // val is the value variable
    checkedBoxes[layer] = val; // add pairs to the object
    $.each( checkedBoxes, function( key, value ) {  // foreach
      $.bbq.pushState(checkedBoxes);  // create a new parameter for each key value
    });
}
trackleft
  • 11
  • 4
0
try this js code..

obj ={}
var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

for(i=0;i<myCars.length;i++){      
        obj.newcars.push(myCars[i]);               
}
mohan
  • 453
  • 1
  • 5
  • 17
0

I've come across this problem and quickly used the two functions as a way to add and remove information to the fragment in the form of an array.

function AddItemToFragment(newItem) {
    var items = $.bbq.getState("itemArray");
    if (!items)
        items = new Array();

    items.push(newItem);
    $.bbq.pushState({itemArrray: items});
}

function RemoveItemFromFragment(itemToRemove) {
    var items = $.bbq.getState("itemArray");

    for (var i = items.length - 1; i >= 0; i--) {
        if (items[i] === itemToRemove)
            items.splice(i, 1);
    }

    $.bbq.pushState(items);
}

I'm not quite happy on how these functions work, there must be a nicer way than creating an array object and rewriting the string. I'll have another look at this later and if I come to anything I'll post it up here.

Martin
  • 1,355
  • 2
  • 14
  • 21