I have a variable $sitedata, an object, that is structured like:

{
    'fname':null,
    'lname':null,
        moduleLevel: {
        bPassed: null,
        bContentComplete: null,
        iNumAttempts: null,
        dDateStart: month + "/" + day + "/" + year,//"6/25/2007",
        dDateEnd: null,
        sTestTaken: null,
        sTestPassed: null,
        sPageViewed: null
    },
    lessonLevel:{
        _0_0:{
            iLessonRef: 0,
            iTopicRef: 0,
            bIsDone: 0,
            sInlineQuiz: '0',
            sPageRef: 0
        }
    }
}



Now, to get the object into a cookie we need to convert it to a querysting,
like:

objectToQueryString($sitedata,"&")

which will return something like:

fname=benjamin&lname=sterling etc....

If used like below:

$.cookie('myCookie', objectToQueryString(myObject,"&"));

Now our cookie is set, now we want to grab it again:

var tempSiteData = $.cookie('myCookie');

now we turn our querysting (fname=benjamin&lname=sterling)

myObject = queryStringToObject(tempSiteData);

into:

{
fname :'benjamin',
lname :'sterling',
etc...
}

Let me know if you understand.

On 9/25/07, Potluri <[EMAIL PROTECTED]> wrote:
>
>
>
> Thanks for your great help. But I'm finding hard to know the exact syntax
> of
> how to use these functions. Can you tell me exact syntax of  how to use
> these functions objectToQueryString and  the function queryStringToObject
> given the array name "arrObject" which contains array of objects.
>
> Waiting for your response.
> Thanks in advance.
> Regards,
> Vijay Potluri
>
>
> bmsterling wrote:
> >
> > You can't store an Object/Array into a cookie, you will get the result
> you
> > are currently seeing.  The code I provided with convert your object into
> a
> > querystring, which can be stored, and stores that and when you want to
> > retrieve it, you pass the returned cookie value to the other function
> and
> > it
> > changes it back to an Object/Array.
> >
> > On 9/25/07, Potluri <[EMAIL PROTECTED]> wrote:
> >>
> >>
> >>
> >> Sorry I didn't understand what you are trying to say. How this solves
> my
> >> problem of storing array of objects into the cookie. Can you try on
> your
> >> side of storing array of objects into cookie, so that later when I try
> to
> >> to
> >> retrive value from cookie I need to get array of objects but not string
> >> which is what happening right now.
> >>
> >> Thanks in advance.
> >>
> >> bmsterling wrote:
> >> >
> >> > Vij,
> >> > I am actually using the following functions in an app that uses the
> >> cookie
> >> > plugin:
> >> >
> >> > /**
> >> >  * @name     sterilizeQueryString
> >> >  * @type     function
> >> >  * @param     {String} input
> >> >  * @desc     Turns a query string into an object
> >> >  * @return     Object b
> >> >  */
> >> > sterilizeQueryString = function(input,splitter,pair){
> >> >     try{
> >> >         if(typeof input != 'string') return null;
> >> >         if(!splitter){
> >> >             splitter = "&";
> >> >         }
> >> >         if(!pair) pair ="=";
> >> >         var a = input.split(splitter), b = Array();
> >> >         for(var i=0; i < a.length; i++)    a[i] = a[i].split(pair);
> >> >
> >> >         for(var i = 0; i < a.length; i++) b[a[i][0]] = a[i][1];
> >> >
> >> >         return b;
> >> >     }
> >> >     catch(e){
> >> >         $.iLogger.log(e.name + ': ' + e.message, 'error',
> >> > 'sterilizeQueryString();');
> >> >     };
> >> > };
> >> >
> >> > objectToQueryString = function( a , joiner, pair) {
> >> >     try{
> >> >         var s = [];
> >> >         if(!pair) pair ="=";
> >> >         if((typeof a == "object")){
> >> >             for(var j in a){
> >> >                 if(typeof a[j] == "object" && a[j]){
> >> >                     s.push(encodeURIComponent(j) + pair + "__" +
> >> > objectToQueryString(a[j],"**","##") +"__");
> >> >                 }
> >> >                 else{
> >> >                     s.push( encodeURIComponent(j) + pair +
> >> > encodeURIComponent( a[j] ) );
> >> >                 }
> >> >             };
> >> >             return s.join(joiner);
> >> >         }
> >> >         else{
> >> >             return a;
> >> >         };
> >> >     }
> >> >     catch(e){
> >> >         $.iLogger.log(e.name + ': ' + e.message, 'error',
> >> > 'objectToQueryString();');
> >> >     };
> >> > };
> >> >
> >> > queryStringToObject = function(s){
> >> >     try{
> >> >         if(s && typeof s == "string"){
> >> >             s = sterilizeQueryString(s);
> >> >             for(var j in s){
> >> >                 if(/__(.*)__/.test(s[j])){
> >> >                     s[j] = s[j].replace(/__/g,"");
> >> >                     s[j] = sterilizeQueryString(s[j],"**","##")
> >> >                 };
> >> >             };
> >> >             return s;
> >> >         }
> >> >         else{
> >> >             return s;
> >> >         };
> >> >     }
> >> >     catch(e){
> >> >         $.iLogger.log(e.name + ': ' + e.message, 'error',
> >> > 'queryStringToObject();');
> >> >     };
> >> > }
> >> >
> >> >
> >> > This is the function that is using the previous functions:
> >> >
> >> > //  if $use is set to cookie, this variable will be used for the
> cookie
> >> > name
> >> > var $cookieName = 'dummyData';
> >> >
> >> > //  if $use is set to cookie, this variable will be used for the
> cookie
> >> > params
> >> > var $cookieParams = {expires: 7};
> >> > /**
> >> >  * @name getSetCookies
> >> >  * @example getSetCookies();
> >> >  * @param {Boolean} getOnly
> >> >  * @desc Purpose of this function is to set and get cookie data
> >> >  * @see objectToQueryString
> >> >  * @see queryStringToObject
> >> >  */
> >> > getSetCookies = function(getOnly){
> >> >     $.iLogger.log('getSetCookies();');
> >> >     try{
> >> >         //
> >> >         if(getOnly){
> >> >             var tempSiteData = $.cookie($cookieName);
> >> >             if(tempSiteData){
> >> >                 $sitedata = queryStringToObject(tempSiteData);
> >> >             }
> >> >             else{
> >> >                 $.cookie($cookieName,
> >> objectToQueryString($sitedata,"&"),
> >> > $cookieParams);
> >> >             }
> >> >         }
> >> >         else{
> >> >             $.cookie($cookieName, objectToQueryString($sitedata,"&"),
> >> > $cookieParams);
> >> >         };
> >> >     }
> >> >     catch(e){
> >> >         $.iLogger.log(e.name + ': ' + e.message, 'error',
> >> > 'getSetCookies();');
> >> >     };
> >> > }; // end : getSetCookies
> >> >
> >> > Don't have time to explain it right now (wanted to at least answer
> >> you),
> >> > but
> >> > if you have questions, I can answer them later on today.
> >> >
> >> > Ben
> >> >
> >> > On 9/25/07, Potluri <[EMAIL PROTECTED]> wrote:
> >> >>
> >> >>
> >> >>
> >> >> I don't see this is a problem with cookie plugin in particular but
> >> this
> >> >> is
> >> >> problem with cookie itself.
> >> >>
> >> >> Suppose this is the array
> >> >> var arr = [];
> >> >> arr.push({name:"vj",rollNo:10});
> >> >> arr.push({name:"kr",rollNo:15});
> >> >>
> >> >> The size of arr is 2 before storing in cookie
> >> >>
> >> >> I tried to store this array object in a cookie as
> >> $.cookie("cookieName",
> >> >> arr), and then later when I tried to  access the length of array
> with
> >> >> cookie
> >> >> as var tempArr= $.cookie("cookieName")--which should give array
> object
> >> >> named
> >> >> "arr".
> >> >>
> >> >> and then I tried to alert length of array as alert(tempArr.length);
> I
> >> was
> >> >> expecting size of array to be 2 but surprisingly its returning 31. I
> >> >> printed
> >> >> the value of tempArr like alert("arr="+ tempArr+"
> >> len="+tempArr.length).
> >> >>
> >> >> it prints arr=[object Object],[object Object]  len=31.
> >> >> When you calculate the each character of [object Object],[object
> >> Object]
> >> >> it
> >> >> returns 31.
> >> >>
> >> >> Can any one of you come up with quick solution of how to store array
> >> of
> >> >> array objects in a cookie.
> >> >>
> >> >> It'll be greatful.
> >> >>
> >> >> Regards,
> >> >> Vijay
> >> >> --
> >> >> View this message in context:
> >> >>
> >>
> http://www.nabble.com/problem-with-cookie-plugin-when-trying-to-store-array-object-in-cookie-tf4516799s15494.html#a12883618
> >> >> Sent from the JQuery mailing list archive at Nabble.com.
> >> >>
> >> >>
> >> >
> >> >
> >> > --
> >> > Benjamin Sterling
> >> > http://www.KenzoMedia.com
> >> > http://www.KenzoHosting.com
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/problem-with-cookie-plugin-when-trying-to-store-array-object-in-cookie-tf4516799s15494.html#a12885387
> >> Sent from the JQuery mailing list archive at Nabble.com.
> >>
> >>
> >
> >
> > --
> > Benjamin Sterling
> > http://www.KenzoMedia.com
> > http://www.KenzoHosting.com
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/problem-with-cookie-plugin-when-trying-to-store-array-object-in-cookie-tf4516799s15494.html#a12887225
> Sent from the JQuery mailing list archive at Nabble.com.
>
>


-- 
Benjamin Sterling
http://www.KenzoMedia.com
http://www.KenzoHosting.com

Reply via email to