Mojo Storage Cookie

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

Typical Uses For A Cookie
Typical uses for a cookie include user configuration for a game or other application, login info, passing a variable from one file to the next, etc.

Cookie Creation, Retrieval & Disposal
When you want to use a cookie, the processes starts out the same regardless of if you have already used the cookie or not.

Creating A New Cookie
Set a variable to a new cookie object. Example: Setting a variable to a cookie object myCookie = new Mojo.Model.Cookie('reversi');

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

If the cookie was already created, "myCookie" will now be linked to it. However, if it does not exist, a new cookie is created.

"myCookie" now has three methods available 1).get 2).put  3).remove that you can use to place data into the cookie. Retrieve data out of the cookie. And dispose of the cookie altogether.

What Can We Store In A Cookie?
A cookie can store only one object. But that object can contain any type of variable. For instance, you can store a string "abc". You can store a number 123. And you can store the contents of a variable. That variable can contain an array which might have many values, but the VARIABLE itself is only one object.

You can also store a javascript object.

Example: creation of a javascript object named "myObject"

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

Putting Something Into The Cookie
Above we created a variable named "myObject" that holds the data. Now, we can store the variable "myObject" in the "myCookie" object we created earlier using the '.put' method.

Example: putting the contents of "myObject" into the cookie "myCookie" myCookie.put(myObject)

Retrieving A Cookies Contents
When you're ready to do something with that cookie's contents you will retrieve the object with the '.get' method.

Example: retrieving a cookies content and placing it in an object someOtherObject = myCookie.get

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

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

Side note:
 * {| style="color:blue; background-color:#ffffcc;" cellpadding="20" cellspacing="0" border="1"


 * When you call the .get method, provided the cookie exists, and has data in it. You get the object that was stored in that cookie returned to you. However, if the cookie does NOT have data in it, the .get returns undefined.

Example: Retrieving a cookies content & testing if the cookies holds anything

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


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

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

Example: removing "myCookie" myCookie.remove.

Javascript Object Notes
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

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

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

Example:  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.

More information on Mojo Cookies can be found here: https://developer.palm.com/content/api/reference/mojo/classes/mojo-model-cookie.html http://www.webos-internals.org/wiki/Tutorials_webOS_Using_Cookies