Working with Cookies

The cookie is a well-known browser feature, created early on to store state or session information. Mojo cookies have a similar purpose, and are technically related to browser cookies, but with an object interface to simplify use by webOS applications. Mojo cookies typically store small amounts of data that will preserve application state and related information, such as preference settings.

Palm webOS creates a fake domain for running applications, based on each application ID. Cookies created by the application code are associated with that domain, so unlike browser cookies, they’ll never be present as part of web requests to other services; they are strictly for local storage.

You should limit cookies to less than 4kB, but can have multiple cookies within an application if needed. You can remove cookies if they are no longer needed and the framework will delete an application’s cookies if the application is removed from the device.

Mojo.Model.Cookie(id) opens the cookie that matches the ID argument or, if there is no match, creates a new cookie with that ID. There are three methods:

get()

Retrieves the object stored in the cookie (if it exists) or returns undefined.

put()

Updates the value of the named cookie with an optional date/time after which the object is deleted.

remove()

Removes the named cookie and deletes the associated data.

The Cookie function and all of its methods are synchronous, unlike Depot and the database functions, making for a simpler calling interface and return handling.

Back to the News: Adding a Cookie

We’ll use a cookie object to save the News preferences. Beyond the basics of creating the cookie and retrieving it on launch, we’ll also add code to update the cookie when the preferences change.

Since the preferences are used in a few different places in the News application, we’ll create the specific News cookie functions in models/cookie.js. The News.Cookie will have an initialize() function, which opens and gets the News cookie if it exists already, or creates the News cookie if not. The cookie’s identifier doesn’t need to be unique outside of this application:

/*  Cookie - NEWS

    Copyright 2009 Palm, Inc.  All rights reserved.

    Handler for cookieData, a stored version of News preferences.
    Will load or create cookieData, migrate preferences and update cookieData
    when called.

    Functions:
    initialize - loads or creates newsCookie; updates preferences with contents
        of stored cookieData and migrates any preferences due version changes
    store - updates stored cookieData with current global preferences
*/

News.Cookie = ({

  initialize: function()  {
    // Update globals with preferences or create it.
    this.cookieData = new Mojo.Model.Cookie("NewsPrefs");
    var oldNewsPrefs = this.cookieData.get();
    if (oldNewsPrefs) {
      // If current version, just update globals & prefs
      if (oldNewsPrefs.newsVersionString == News.versionString)    {
        News.featureIndexFeed = oldNewsPrefs.featureIndexFeed;
        News.featureFeedEnable = oldNewsPrefs.featureFeedEnable;
        News.featureStoryInterval = oldNewsPrefs.featureStoryInterval;
        News.feedUpdateInterval = oldNewsPrefs.feedUpdateInterval;
        News.versionString = oldNewsPrefs.newsVersionString;
        News.notificationEnable = oldNewsPrefs.notificationEnable;
        News.feedUpdateBackgroundEnable = oldNewsPrefs.feedUpdateBackgroundEnable;
      } else {
        // migrate old preferences here on updates of News app
      }
    }

    this.storeCookie();

  },

  //  store - function to update stored cookie with global values
  storeCookie: function() {
    this.cookieData.put(    {
      featureIndexFeed: News.featureIndexFeed,
      featureFeedEnable: News.featureFeedEnable,
      feedUpdateInterval: News.feedUpdateInterval,
      featureStoryInterval: News.featureStoryInterval,
      newsVersionString: News.versionString,
      notificationEnable: News.notificationEnable,
      feedUpdateBackgroundEnable: News.feedUpdateBackgroundEnable
    });
  }

});

After creating the cookie in our sample, this.cookie.get() is called to retrieve it. If the cookie exists, the global preferences are updated with the stored values.

The second function will update the stored cookie with the current values in the global preferences. It’s called from the initialize() method in the case where the cookie was first created and also as a future provision when preferences need to be migrated when a new version of the application changes the preferences.

The cookie should be first retrieved at application launch:

StageAssistant.prototype.setup = function() {

    // initialize the feeds model
    this.feeds = new Feeds();
    this.feeds.loadFeedDb();

    // load preferences and globals from saved cookie
    News.Cookie.initialize();

    // Set up first timeout alarm
    this.setWakeup();

    this.controller.pushScene("feedList", this.feeds);

};

The storeCookie() method is called anytime the preferences change. For example, the deactivate() method in preferences-assistant.js will update the cookie when the Preferences scene is popped:

// Deactivate - save News preferences and globals
PreferencesAssistant.prototype.deactivate = function() {
    News.Cookie.storeCookie();
};

The cookie.remove() method is straightforward, but you may not have any reason to use it at all. With News, the preferences are always retained unless the application is deleted from the device, in which case the storage is recovered by the system. If you are using cookies for temporary storage, you should remove them when they are no longer needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.45.29