Mojo Storage Cookie

From WebOS Internals
Revision as of 13:30, 30 August 2009 by Rboatright (talk | contribs) (2)
Jump to navigation Jump to search

Cookies are generally used to hold small amounts of information. A cookie may hold ONE OBJECT. That object can be any javascript object (we will see some options later) but the cookie can hold only one.

Typical uses for a cookie include User configuration for a game or other app, login info, etc.

When you want to use a cookie, the processes starts out the same no matter if you have already used a cookie or now.

You must set a variable to a cookie object. This is simple:

myCookie = new Mojo.Model.Cookie('reversi');

the name inside the call to Mojo.Model.Cookie names a unique cookie that will be linked to YOUR APP.

If the cookie already exists, myCookie is linked to it. If it does not exist, a new cookie is created.

myCookie now has three methods available. .get .put and .remove

If you call .get, if the cookie exists, and has data in it, you get the object that was stored in the cookie returned to you. If the cookie does NOT have data in it, the .get returns undefined.

So, you can do:

cookieObject = this.cookie.get()
if (cookieObject === undefined)  {
    //do something that sets defaults
} else {
   //  do something with the data in cookieObject.
}

What can we store in a cookie? A cookie can store one object. You can store a string "abc". You can store a number 123, you can store the contents of a variable. That variable can contain an array which might have many values, but the VARIABLE is one object.

You can also store a javascript object. For example:

var myObject = {"name": "Fred", "Address": "123 w 4th St", "City": "Somewhere" }

now, we can store myObject in a cookie using the other call to the cookie we created earlier:

myCookie.put(myObject)  

stores myObject into the cookie.

Now, we can retrieve the object with

someOtherObect = myCookie.get() 

now, we can just refer to that object's properties...

someOtherObject.name will now be "Fred" someOtherObject.city will be Somewhere.

So, create an object. Get it. Check if it's undefined. If it is, define it. Otherwise, grab the data and go on. If the data changes, PUT IT BACK.

To get rid of a cookie that you no longer want, call the remove method.

myCookie.remove. 

Simple. Now, a couple of side notes. As was said above, you can store ANY object in a cookie. So, If all you want to do is store a single high score in a cookie, you can simply do

myCookie = new Mojo.Model.Cookie('score');
myCookie.put(highScore);

Your app can create more than one cookie. You could for example do:

levelCookie = new Mojo.Model.Cookie('level');
levelCookie.put(highScore);
scoreCookie = new Mojo.Model.Cookie('score');
scoreCookie.put(highScore);

Instead of putting score and level into a single object as above. There is no particular advantage or disadvantage to either technique, it is a style preference. Note that in WEB programming, a site creating more than one cookie is considered impolite. This is not an issue inside the phone. In any case, the cookies are tied directly to your app, and the data is not available to other apps.