No, these cookies won't increase your Mojo. Unless you define "Mojo" as the ability to effortlessly store small amounts of local data on webOS devices.
The next few articles will take a look at the local storage capabilities of webOS, as well as data transfer from remote servers using Ajax. The first function we're going to look at is cookies (mmm...cookies).
webOS provides a cookie API, a "simplified interface to browser cookies", intended to be used as a "single object store for small amounts of data" such as application preferences, version numbers and other state information. Palm encourages the storage of cached data for offline access as a way to make your applications more flexible.
Mojo cookies are related to browser cookies, but accessible through an object interface to simplify usage by applications.
Access to traditional browser-based cookies is based on the web page's domain. webOS creates a "fake domain" for running applications that is based on the application's ID. This limits access to the cookie to the application itself; the cookie can never be accessed by an external web page or service.
Palm suggests limiting cookies to less than 4 kilobytes, but allows multiple cookies per application if needed. Cookies can be deleted and will automatically be erased if the application that created them is deleted.
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 this cookie if it exists or returns undefined.
- put() updates the values of the object with an optional date/time after which to expire (delete)the object.
- remove() removes the named cookie and deletes the associated data.
The Cookie function and methods are synchronous (that is, you do not have to define a callback function to receive the returned data), but your application will have to wait for the data to be returned before continuing. However; this time should be negligible.
Click here for an example of setting up a cookie.
And that's all you need to know about cookies. Unless you're this guy.
Tomorrow we'll look at Mojo Depot - a simplified interface to the HTML5 Database storage API.
This is one of many daily development-related webOS articles. Grab the RSS feed to stay in the know!
Much of the information in this article was presented in Chapter 6 of Palm webOS by Mitch Allen.
0 Comments