AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 531


Note that you can save multiple key-value pairs in a particular store like our
"storageLocker" above.
Retrieval is performed similarly. First, fetch the DOM element being used with the
persistence behavior. Next, call the load() method, passing it the string used as the store
(in this case "storageLocker"). Finally, use getAttribute(key) to retrieve the value at
the passed key.

var persistObj = document.getElementById("persistThis");
persistObj.load(storageLocker);
var value = persistObj.getAttribute(key);

The third method for persistence would be using the Flash Player’s SharedObject and
bridging into JavaScript, as we have done for cross-domain requests and socket communication
previously in this chapter. This approach is quite appealing because it is transportable between
any browser that can use the Flash Player. This means that you can persist data between
Internet Explorer and Firefox on the same machine, very powerful and very scary to the
privacy minded! Second, we note the scheme typically has a decent size limit of 100KB, though
it can be tuned much higher if the user is prompted. Finally, the storage is not known by many
users and thus is rarely cleared by them. Of course, it has the obvious downside of requiring
Flash in order to work and then relying on the bridge between the two technologies.
The ActionScript code to create a storage system in Flash is quite simple and is shown
here in its entirety:

import flash.external.ExternalInterface;
class AjaxTCRStorage{
static var mySharedObject : SharedObject;

static function add(key, value)
{
mySharedObject.data[key] = value;
mySharedObject.flush();
}
static function get(key, value)
{
return mySharedObject.data[key];
}
static function clear()
{
mySharedObject.clear();
}
static function remove(key)
{
delete mySharedObject.data[key];
}
static function getAll()
{
return mySharedObject.data;
}
static function main()
{
mySharedObject=SharedObject.getLocal("AjaxTCRData");
Free download pdf