AS2: SharedObjectManager

View Documentation
Download Class

A simple utility to manage shared objects and their properties.

Actionscript:
  1. import mx.events.EventDispatcher;
  2. import mx.utils.Delegate;
  3.  
  4. /**
  5. * A simple utility to manage shared objects and their properties.
  6. *
  7. * @usage
  8. * <code>
  9. * <pre>
  10. import com.reintroducing.utils.SharedObjectManager;
  11. var som:SharedObjectManager = new SharedObjectManager("userData");
  12. som.setProperty("hasVisited", "yes");
  13. trace(som.getProperty("hasVisited"));
  14. * </pre>
  15. * </code>
  16. *
  17. * @author Matt Przybylski [http://www.reintroducing.com]
  18. * @version 1.0
  19. */
  20.  
  21. class com.reintroducing.utils.SharedObjectManager
  22. {
  23. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  24.     
  25.     private var dispatchEvent:Function;
  26.    
  27.     private var _so:SharedObject;
  28.    
  29. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  30.    
  31.     public static var DEFAULT_NAME:String = "com.reintroducing.utils.SharedObjectManager";
  32.    
  33.     public var addEventListener:Function;
  34.     public var removeEventListener:Function;
  35.    
  36. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  37.    
  38.     /**
  39.      * Creates a new instance of the SharedObjectManager class.
  40.      *
  41.      * @usage <pre><code>var som:SharedObjectManager = new SharedObjectManager($name);</code></pre>
  42.      *
  43.      * @param $name A string value representing the shared object to create/retrieve from the user's hard drive
  44.      */
  45.    
  46.     public function SharedObjectManager($name:String)
  47.     {
  48.         EventDispatcher.initialize(this);
  49.        
  50.         this._so = SharedObject.getLocal($name);
  51.         this._so.onStatus = Delegate.create(this, onStatus);
  52.     }
  53.    
  54. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  55.    
  56.     private function onStatus($evt:Object):Void
  57.     {
  58.         switch ($evt.code)
  59.         {
  60.             case "SharedObject.Flush.Success":
  61.                 this.dispatchEvent({type: "onSOManagerSuccess", target: this});
  62.                 break;
  63.            
  64.             case "SharedObject.Flush.Failed":
  65.                 this.dispatchEvent({type: "onSOManagerFailed", target: this});
  66.                 break;
  67.         }
  68.     }
  69.    
  70. //- PUBLIC METHODS ----------------------------------------------------------------------------------------
  71.    
  72.     /**
  73.      * Sets a "cookie" (property/value pair) object in the current shared object and saves it to the user's hard drive.
  74.      *
  75.      * @usage <pre><code>som.setProperty("hasVisited", "yes");</code></pre>
  76.      *
  77.      * @param $name A string that represents the name of the property to be stored in the shared object
  78.      * @param $value A string that represents the value of the property to be stored in the shared object
  79.      *
  80.      * @return Nothing
  81.      */
  82.    
  83.     public function setProperty($name:String, $value:Object):Void
  84.     {
  85.         this._so.data[$name] = $value;
  86.         this._so.flush();
  87.     }
  88.    
  89.     /**
  90.      * Returns the value for the requested property.
  91.      *
  92.      * @usage <pre><code>som.getProperty("hasVisited");</code></pre>
  93.      *
  94.      * @param $name A string that represents the name of the property you want to retrieve
  95.      *
  96.      * @return String
  97.      */
  98.    
  99.     public function getProperty($name:String):String
  100.     {
  101.         return this._so.data[$name];
  102.     }
  103.    
  104.     /**
  105.      * Clears the current shared object.
  106.      *
  107.      * @usage <pre><code>som.clear();</code></pre>
  108.      *
  109.      * @return Nothing
  110.      */
  111.    
  112.     public function clear():Void
  113.     {
  114.         this._so.clear();
  115.     }
  116.    
  117. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  118.    
  119.    
  120.    
  121. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  122.    
  123.    
  124.    
  125. //- HELPERS -----------------------------------------------------------------------------------------------
  126.    
  127.     public function toString():String
  128.     {
  129.         return "com.reintroducing.utils.SharedObjectManager";
  130.     }
  131.    
  132. //- END CLASS ---------------------------------------------------------------------------------------------
  133. }

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

1 Comment

[...] 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 [...]

Leave a comment

(required)

(required)