4

I'm opening a new thread based on this how to store an array in jquery cookie?. I'm using function from almog.ori :

var cookieList = function(cookieName) {
//When the cookie is saved the items will be a comma seperated string
//So we will split the cookie by comma to get the original array
var cookie = $.cookie(cookieName);
//Load the items or a new array if null.
var items = cookie ? cookie.split(/,/) : new Array();

//Return a object that we can use to access the array.
//while hiding direct access to the declared items array
//this is called closures see http://www.jibbering.com/faq/faq_notes/closures.html
return {
    "add": function(val) {
        //Add to the items.
        items.push(val);
        //Save the items to a cookie.
        $.cookie(cookieName, items);
    },
    "clear": function() {
        //clear the cookie.
        $.cookie(cookieName, null);
    },
    "items": function() {
        //Get all the items.
        return items;
    }
  }
}

to grab an array of index element on click event :

var list = new cookieList("test");
$(img).one('click', function(i){
while($('.selected').length < 3) {
    $(this).parent()
        .addClass("selected")
        .append(setup.config.overlay);

    //$.cookie(setup.config.COOKIE_NAME, d, setup.config.OPTS);
    var index = $(this).parent().index();

    // suppose this array go into cookies.. but failed
    list.add( index );
    // this index goes in array
    alert(list.items());

    var count = 'You have selected : <span>' + $('.selected').length + '</span> deals';
    if( $('.total').length ){
        $('.total').html(count);
    }

} 
});

When I getting list of items, it return null but when I alert in onclick event, eventually it push value into that array. But when I'd tried to retrieve back cookie, it return null. Help please...

Community
  • 1
  • 1
fadzril
  • 169
  • 2
  • 2
  • 7

2 Answers2

10

This line:

$.cookie(cookieName, items);

Should create the string from the array as well, like this:

$.cookie(cookieName, items.join(','));

This is so that when loading the array via cookie.split(/,/), it gets a string it expects (comma-delimited).

almog.ori
  • 7,839
  • 1
  • 35
  • 49
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    Another possibility could be to use `window.JSON.stringify` if available. Makes maybe more sense on more complex `Arrays/Objects`. – jAndy Aug 02 '10 at 11:45
  • @jAndy - I agree in larger cases, but in this case it's an array of integers, so no need to over-complicate it :) – Nick Craver Aug 02 '10 at 12:37
1
var cookieList = function(cookieName) {

    var cookie = Cookies.get(cookieName);
    var items = cookie ? cookie.split(/,/) : new Array();

    return {
        "add": function(val) {
            items.push(val);
            Cookies.set(cookieName, items.join(','), {expires: new Date(2020, 0, 1)});
        },
        "remove": function(val) {
            indx = items.indexOf(val);
            if (indx != -1)
                items.splice(indx, 1);
            Cookies.set(cookieName, items.join(','), {expires: new Date(2020, 0, 1)});
        },
        "clear": function() {
            items = null;
            Cookies.set(cookieName, null, {expires: new Date(2020, 0, 1)});
        },
        "items": function() {
            return items;
        }
    }
};
AntonChe
  • 11
  • 2