LocalPersistenceMap
When developing browser based Flex application SharedObject provides just about everything one needs to facilitate local persistence of application data. SharedObject is restricted by certain rules governed by the Flash Player Security Model, however it is very efficient for general use.
The data property of a SharedObject provides read/write access to the underlying data which is persisted to the SharedObject. Personally, I prefer to have a consistent API available when working with dynamic objects, thus I developed the IMap interface.
LocalPersistenceMap provides an IMap implementation into the data property of a SharedObject instance. It allows clients to work with the underlying data of a SharedObject just as one would with a HashMap, ResourceMap, etc.
Below is a basic example which demonstrates how LocalPersistenceMap can be utilized to provide an IMap implementation into a SharedObject:
var map:IMap = new LocalPersistenceMap("test", "/");
map.put("username", "efeminella");
map.put("password", "43kj5k4nr43r934hcr34hr8h3");
map.put("admin", true);
The LocalPersistenceMap constructor creates a reference to a local SharedObject based on the specified identifier and optional local path. A reference to the underlying SharedObject can also be retrieved via the sharedObjectInstance accessor.
In addition to the new LocalPersistenceMap, I have also packaged the Collections API which can be downloaded here. Complete documentation and code examples for the Collections API are also available here.
March 10th, 2008 at 4:57 am
Nice library.
I am wondering if you can store more complex information aswell. It would be nice for example if you would build in a check to see if complex objects have been registered before putting them in, something like this (from the top of my head):
function put(key:String, object:Object):void
{
var ba:ByteArray = new ByteArray();
ba.writeObject(object);
ba.position = 0;
var newObject:Object = ba.readObject();
if (describeType(object).@name != describeType(newObject).@name)
{
throw new Error (”If you save complex objects, make sure you use registerClassAlias to make sure they can be stored”);
};
};
It is just an idea
Greetz Erik
March 10th, 2008 at 8:30 am
Good point, initially I thought of this as well, however I opted not to perform the check as the IMap implementation is only intended to provide an interface into the data. The call to registerClassAlias is assumed by the client implementation as the internal check makes the implementation quite expensive to perform when it is not needed if the object is a value type.
With that being said I realized some people may want to perform the check internally therefore I did not define the LocalPersistenceMap as final so developers can extend to implement additional functionality such as this as needed.
Thanks,
Eric
October 17th, 2008 at 2:35 am
Hi Eric,
Thanks for this great API!
But it seems like Collections.zip archive isn’t available at http://code.ericfeminella.com/downloads/Collections.zip
Could you please fix or update the link?