Tutorials webOS Using Cookies

From WebOS Internals
Jump to navigation Jump to search

Using Cookies To Move Variables To Different Scenes

This is my non-technical take on how to make cookies for the WebOS

In life if you want to make a special cookie there's an order you need to follow.

  1. Name your cookie
  2. Make your cookie and put it into a container
  3. Move your cookie container to the new room
  4. Get your cookies and share them


In the following example I want to move a variable stored in “this.numBalance” over to my second scene.


The javascript file for the first scene (first-assistant.js) holds the variable (this.numBalance) I want to move and use in the next scene (second-assistant.js). The event that will push the variable to the next scene is a button click, so I would place the cookie code into my " FirstAssistant.prototype.handleClicked = function".


Here’s the variable to be pushed to the next scene

this.numBalance = this.nBalancemodel.value;


Next create a cookie named this.numBalanceCookie in the cookies system and then associate the cookie with the property of the current scene. All this does is name your cookie. No values have been placed inside of it.

this.numBalanceCookie = new Mojo.Model.Cookie("numBalance");


Next put the this.numBalance variable into the this.numBalanceCookie. In this example I'm using an if statement to first check if the variable is not empty then put its value into the numBalanceCookie.

if (this.numBalance != ) {
 this.numBalanceCookie.put(this.numBalance);
}


In the second-assistant.js file's setup function (Second Assistant.prototype.setup = function) retrieve and use the cookie’s variable.


You need to still create a cookie named this.numBalanceCookie in this scene’s cookies system and associate it to the current scene.

this.numBalanceCookie = new Mojo.Model.Cookie("numBalance");


Now you can retrieve the cookie’s (this.numBalanceCookie) value with the get function and place the value into a new variable (this.numBalance).

this.numBalance = this.numBalanceCookie.get();


Now you can use the this.numBalance variable


In this example we used two of the three methods available in the Mojo.Model.Cookie Class

The methods available are:

get()
Returns the object stored in this cookie, or undefined if no such cookie exists
put()
Creates or updates the value of this cookie.
remove()
Deletes this cookie.



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