AS3: SharedObjectManager
View Documentation
Download Class
A simple utility to manage shared objects and their properties. This is the same as the AS2 version but adjusted for use in AS3.
Actionscript:
-
/**
-
* A simple utility to manage shared objects and their properties.
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.utils.SharedObjectManager;
-
var som:SharedObjectManager = new SharedObjectManager("userData");
-
som.setProperty({hasVisited: "yes"});
-
trace(som.getProperty("hasVisited"));
-
* </pre>
-
* </code>
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
-
package com.reintroducing.utils
-
{
-
import flash.events.Event;
-
import flash.events.EventDispatcher;
-
import flash.events.NetStatusEvent;
-
import flash.net.SharedObject;
-
-
public class SharedObjectManager extends EventDispatcher
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
private var _so:SharedObject;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
public static const DEFAULT_NAME:String = "com.reintroducing.utils.SharedObjectManager";
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
/**
-
* Creates a new instance of the SharedObjectManager class.
-
*
-
* @usage <pre><code>var som:SharedObjectManager = new SharedObjectManager($name);</code></pre>
-
*
-
* @param $name A string value representing the shared object to create/retrieve from the user's hard drive
-
*/
-
-
public function SharedObjectManager($name:String):void
-
{
-
this._so = SharedObject.getLocal($name);
-
this._so.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
private function onStatus($evt:NetStatusEvent):void
-
{
-
switch ($evt.info.code)
-
{
-
case "SharedObject.Flush.Success":
-
this.dispatchEvent(new Event(Event.COMPLETE));
-
break;
-
-
case "SharedObject.Flush.Failed":
-
this.dispatchEvent(new Event(Event.CANCEL));
-
break;
-
}
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* Sets a "cookie" (property/value pair) object in the current shared object and saves it to the user's hard drive.
-
*
-
* @usage <pre><code>som.setProperty({hasVisited: "yes"});</code></pre>
-
*
-
* @param $obj An object that represents the property/value pair to be stored in the shared object
-
*
-
* @return Nothing
-
*/
-
-
public function setProperty($obj:Object):void
-
{
-
var prop:String;
-
-
for (prop in $obj)
-
{
-
this._so.data[prop] = $obj[prop];
-
this._so.flush();
-
}
-
}
-
-
/**
-
* Returns the value for the requested property.
-
*
-
* @usage <pre><code>som.getProperty("hasVisited");</code></pre>
-
*
-
* @param $name A string that represents the name of the property you want to retrieve
-
*
-
* @return String
-
*/
-
-
public function getProperty($name:String):String
-
{
-
return this._so.data[$name];
-
}
-
-
/**
-
* Clears the current shared object.
-
*
-
* @usage <pre><code>som.clear();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function clear():void
-
{
-
this._so.clear();
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public override function toString():String
-
{
-
return "com.reintroducing.utils.SharedObjectManager";
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.











Cheers, nice work