WebOS Boston Developing for WebOS

28Aug/093

Working with Cookies

Post to TwitterPost to FacebookPost to DiggPost to Reddit

Over the last few days I have been working with the Cookie as a method for storage on the Palm Pre. I looked in the WebOS Book and there was a nice cookie class that could be used. This will not be a complete walk-through like the last tutorials have been. This will show you how to create a cookie object and how to interact with it so that you can store your application preferences. You can store any type of data you would like in a cookie. It does not seem to be like the Depot where you can *only* store objects.

At the time of writing I have found an issue with using cookies so be sure to read the second to last paragraph.
Below is the class that was taken from the WebOS Book which we will be using for our Cookie object.

// Globals
WBPrefs = {};
WBPrefs.someOtherValue = false;
WBPrefs.versionString = "0.0.1";

function WBCookie()
{

}

WBCookie.prototype.initialize = function()
{
    this.cookie = new Mojo.Model.Cookie('cookie_name');
    this.oldPrefs = this.cookie.get();
    if(this.oldPrefs)
    {
        // If current version, just update globals & prefs
        if(this.oldPrefs.versionString == WBPrefs.versionString)
        {
            WBPrefs.someOtherValue = this.oldPrefs.someOtherValue;
            WBPrefs.versionString = this.oldPrefs.versionString;
        }
        else
        {
            // migrate old preferences here on updates of FreshBooks App.
        }
    }

    this.storeCookie();
};

WBCookie.prototype.storeCookie = function()
{
    this.cookie.put(
        {
            someOtherValue: WBPrefs.someOtherValue,
            versionString: WBPrefs.versionString
        }
    );
};

This code is pretty straight forward. It creates a WBCookie object which actually interacts with the Mojo.Model.Cookie object. You can see the initialize command does a few things. First it creates a new Mojo.Model.Cookie object with the name of your cookie and stores it in a property named this.cookie. It creates another property called this.oldPrefs which will store the values that are pulled out of the cookie. It is called oldPrefs because they are the values that were last used for the application. It then makes sure that oldPrefs is a valid object. Once it knows we have a valid result we can then compare the versionString stored in the cookie with our application cookie versionString. This allows us to properly migrate preferences if we ever change the structure of the cookie. It then calls the this.storeCookie method which saves the current application Preferences into the cookie. When you are using this method the cookie is updated everytime you update one of the WBPrefs values. I am not entirely sure what causes it to update with every preference change it must be some magic done by the framework.

Now that we have the cookie class we will need to create a scene assistant which will make use of the cookie. We will very simply create a view with a text field and a button. You will be able to change the value in the textbox and tap the button to update the cookie value. We will start with the following code.

function FirstuseAssistant() {

}

FirstAssistant.prototype.setup = function() {
    this.cookie = new WBCookie();
    this.cookie.initialize();

    WBPrefs.someOtherValue = "Joseph Crawford";
}

FirstAssistant.prototype.activate = function(event) {

}

FirstAssistant.prototype.deactivate = function(event) {
        this.cookie.storeCookie();
}

FirstAssistant.prototype.cleanup = function(event) {

}

In the assistant code above you can see that we simply update the value of WBPrefs.someOtherValue which updates the cookie. Next time we run the application the value will have been retained for our use again. This comes in very handy for detecting which scene you should show. For instance if your application will require a user to login you can check the cookie for the username / password and if they do not exist then show the sign-in scene otherwise show your main scene.

I am not sure what is happening whether palm has a bug in their code or if it is just my setup however when testing this all out I noticed that my cookies are retained on the emulator. I have deleted the application from the emulator by holding the option key while tapping and I have also used the Komodo Add-On. However my cookie seems to be retained across application installs and it should not be. According to Palm's Documentation the cookies and databases related to an application should be removed from the device when the application is removed. I personally have not tried it on the device but others have confirmed they have had their cookies be retained on the device as well. Until this issue is fixed I really do not see a real use for cookies. The data should not be retained across application installs. Have you ever had the issue of a cookie being retained like this? The only way I have found to test to make sure it was all working is to add a this.cookie.remove() before we set the cookie. This will make the app remove the cookie whenever it is launched. Obviously that is not production worthy code because the application settings would be removed on every launch. It did however show me that my cookie was being retained and that my other code was working properly.

The code above is not a full example it just illustrates how to use the cookie model in Mojo. You will have to do some of your own coding using these methods in order to use it properly.

About Joseph Crawford

A web developer with over 10 years of experience who has been working on high traffic e-commerce and social networking sites. Specializing in website development using PHP it only took time before he ventured into the WebOS arena. You can visit his website to find out more about him.
Tagged as: , , , , 457 Views Leave a comment
Comments (3) Trackbacks (0)
  1. I store one cookie, which records which of three variants of a scene is shown. This is retained across application deletes, on both the simulator and device. For my case, this is useful behavior, but I expect Palm to fix it at some point.

  2. Thank you for verifying that it is still retained on the device. I was almost starting to think that there was something wrong with my emulator installation until someone told me they had the same issue.

    I do hope that palm fixes this or *gives* the option to remove the cookie. It is not very useful in most situations when the cookie is retained but you have a unique situation where it is useful.

    I hope they do fix this but maybe they could make it an option that you set whether or not to have the cookie deleted.

    Though I think they will just remove it because of their statements already made that all data associated with an application will be removed from the device when the application is removed.

  3. Cookie is not been deleted at application Delete and its a known bug which is mentioned in the forum by Palm Employees


Leave a comment


No trackbacks yet.

Navigation

Calendar

<<Jul 2010>>
MTWTFSS
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1

Most Viewed

Categories

StatPress

Visits Today: 3
Total Visits: 3989
Guests Online: 0
Members Online: 0

Archives

Tag Cloud

authors books Bugs Cookie Depot Development directions JavaScript Lists Meetings Mojo Organization Palm Storage Tutorial WebOS WebOS Boston