AS2: SharedObjectManager
View Documentation
Download Class
A simple utility to manage shared objects and their properties.
Actionscript:
-
import mx.events.EventDispatcher;
-
import mx.utils.Delegate;
-
-
/**
-
* 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
-
*/
-
-
class com.reintroducing.utils.SharedObjectManager
-
{
-
//- PRIVATE VARIABLES -------------------------------------------------------------------------------------
-
-
private var dispatchEvent:Function;
-
-
private var _so:SharedObject;
-
-
//- PUBLIC VARIABLES --------------------------------------------------------------------------------------
-
-
public static var DEFAULT_NAME:String = "com.reintroducing.utils.SharedObjectManager";
-
-
public var addEventListener:Function;
-
public var removeEventListener:Function;
-
-
//- 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)
-
{
-
EventDispatcher.initialize(this);
-
-
this._so = SharedObject.getLocal($name);
-
this._so.onStatus = Delegate.create(this, onStatus);
-
}
-
-
//- PRIVATE METHODS ---------------------------------------------------------------------------------------
-
-
private function onStatus($evt:Object):Void
-
{
-
switch ($evt.code)
-
{
-
case "SharedObject.Flush.Success":
-
this.dispatchEvent({type: "onSOManagerSuccess", target: this});
-
break;
-
-
case "SharedObject.Flush.Failed":
-
this.dispatchEvent({type: "onSOManagerFailed", target: this});
-
break;
-
}
-
}
-
-
//- PUBLIC 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 $name A string that represents the name of the property to be stored in the shared object
-
* @param $value A string that represents the value of the property to be stored in the shared object
-
*
-
* @return Nothing
-
*/
-
-
public function setProperty($name:String, $value:Object):Void
-
{
-
this._so.data[$name] = $value;
-
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 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.











[...] I went ahead and threw in the secure parameter as well in case anyone needs it. I've updated the AS2 and AS3 versions to version 1.1 and you can grab the updated files at the respective links. I've [...]