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

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

2 Comments

Cheers, nice work

Thanks, very useful!

Leave a comment

(required)

(required)