Difference between revisions of "Mojo Storage Cookie"

From WebOS Internals
Jump to navigation Jump to search
(2)
Line 1: Line 1:
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.  
+
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 include User configuration for a game or other app, login info, etc.  
+
===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.  
  
When you want to use a cookie, the processes starts out the same no matter if you have already used a cookie or now.  
+
===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 cookie object.   
 +
<br>''Example: Setting a variable to a cookie object''
 +
myCookie = new Mojo.Model.Cookie('reversi');
  
You must set a variable to a cookie object.   This is simple:
+
Here the name inside the call to Mojo.Model.Cookie names a unique cookie that will be linked to YOUR APPLICATION.  
  
  myCookie = new Mojo.Model.Cookie('reversi');
+
If the cookie was already created, "myCookie" will now be linked to it. However, if it does not exist, a new cookie is created.  
  
the name inside the call to Mojo.Model.Cookie names a unique cookie that will be linked to YOUR APP.  
+
"myCookie" now has three methods available
 +
;#.get 
 +
;#.put 
 +
;#.remove
  
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
+
====What Can We Store In A Cookie?====
  
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.
+
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.
  
So,  you can do:  
+
You can also store a javascript object.  
  
cookieObject = this.cookie.get()
+
<br>''Example: creation of a javascript object named "myObject"''
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. 
+
  var myObject = {"name": "Fred", "Address": "123 w 4th St", "City": "Somewhere" }
  
You can also store a javascript object.  For example: 
 
  
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.
  
now, we can store myObject in a cookie using the other call to the cookie we created earlier: 
 
  
 +
<br>''Example: putting the contents of "myObject" into the cookie "myCookie"''
 
  myCookie.put(myObject)   
 
  myCookie.put(myObject)   
  
stores myObject into the cookie.
+
====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.
  
Now, we can retrieve the object with
 
  
  someOtherObect = myCookie.get()  
+
<br>''Example: retrieving a cookies content and placing it in an object''
 +
  someOtherObject = myCookie.get()  
  
now, we can just refer to that object's properties...  
+
Now, we can just refer to that object's properties...  
  
 
someOtherObject.name will now be "Fred"    someOtherObject.city will be Somewhere.   
 
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.
 
  
 +
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''.
 +
 +
<br>''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.
 +
}
 +
|}
 +
 +
 +
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''.
 +
 +
<br>''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. 
 +
#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.
 +
 +
====Throwing The Cookie Away====
 
To get rid of a cookie that you no longer want, call the remove method.  
 
To get rid of a cookie that you no longer want, call the remove method.  
  
 +
<br>''Example: removing "myCookie"''
 
  myCookie.remove.  
 
  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
 
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
  
 +
<br>''Example: ''
 
  myCookie = new Mojo.Model.Cookie('score');
 
  myCookie = new Mojo.Model.Cookie('score');
 
  myCookie.put(highScore);
 
  myCookie.put(highScore);
Line 63: Line 100:
 
Your app can create more than one cookie.  You could for example do:  
 
Your app can create more than one cookie.  You could for example do:  
  
 +
<br>''Example: ''
 
  levelCookie = new Mojo.Model.Cookie('level');
 
  levelCookie = new Mojo.Model.Cookie('level');
 
  levelCookie.put(highScore);
 
  levelCookie.put(highScore);

Revision as of 20:45, 7 September 2009

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 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


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:

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.
}


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.
}


  1. So, create an object.
  2. Get it.
  3. Check if it's undefined. If it is, define it.
  4. Otherwise, grab the data and go on.
  5. 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.