The SWXVars class

Following the previous post about my SWX implementation within my custom services manager/gateway, I thought it'd be good to see how this would work within Flash. Playing with loading the external swf locally (which for now doesn't work on my server here, but with PHP5 installed it shouldn't pose a problem) I found it a little awkward to load data in random movieclips, so I've created a wrapper class, SWXVars. It allows for eventListeners to track loading progress and should make it a lot easier to do something with the data once loaded.
Setting up the SWXVars within your fla looks something like this:

Actionscript:
  1. // import the SWXVars class
  2. import nl.onload.phpflash.SWXVars;
  3.  
  4. // setup eventlisteners
  5. function onLoadInit(evtObj:Object)
  6. {
  7.  var record = evtObj.target.getRecord(0);
  8.  trace(record.ID +":"+record.post_title);
  9. }
  10.  
  11. // create a new SWXVars instance
  12. swx = SWXVars.createInstance(this, "swx_mc", 1);
  13.  
  14. // setup parameters
  15. swx.gateway = "http://localhost/flashphp/php/gateway.php";
  16. swx.parameters = {service:"getPosts", page:1, perpage:1};
  17. swx.method = "GET";
  18.  
  19. // add listeners
  20. swx.addEventListener("onLoadInit", this);
  21.  
  22. // load data
  23. swx.loadData();

The above should illustrate the usage of the SWXVars class. The Class itself is descibed below:

Actionscript:
  1. // allows for dispatching events
  2. import nl.onload.utils.DispatcherMovieClip;
  3.  
  4. dynamic class nl.onload.phpflash.SWXVars extends DispatcherMovieClip
  5. {
  6.  
  7.  //-----------------------------------------------------------
  8.  // variables
  9.  //-----------------------------------------------------------
  10.  
  11.  // requirement for dynamically creating the libraryitem
  12.  // via http://www.peterjoel.com/blog/?archive=2004_01_01_archive.xml
  13.  static var symbolName:String = "__Packages.nl.onload.phpflash.SWXVars";
  14.     static var symbolOwner:Function = SWXVars;
  15.  
  16.  // for debugging purposes
  17.  private var className:String = "nl.onload.phpflash.SWXVars";
  18.  
  19.  // private variables accessible via getters/setters
  20.  private var __gateway:String;
  21.  private var __method:String = "POST";
  22.  private var __parameters:Object;
  23.  private var __result_mc:MovieClip;
  24.  private var __result:Object;
  25.  private var __loadcompleted:Boolean = false;
  26.   
  27.  //-----------------------------------------------------------
  28.  // class methods
  29.  //-----------------------------------------------------------
  30.  
  31.  // constructor
  32.  function SWXVars()
  33.  {
  34.  }
  35.   
  36.  // static function createInstance to create a new SWXVars instance
  37.  static function createInstance(path, instanceName, depth, initObj):SWXVars
  38.  {
  39.   path.attachMovie(symbolName, instanceName, depth, initObj);
  40.   var SWXVarsInstance:SWXVars = path[instanceName];
  41.   return SWXVarsInstance;
  42.  }
  43.  
  44.  //-----------------------------------------------------------
  45.  // getters/setters
  46.  //-----------------------------------------------------------
  47.  
  48.  // gateway path
  49.  public function set gateway(gateway_str:String):Void
  50.  {
  51.   var domain:String = gateway_str.split("/").slice(0,3).join("/");
  52.   System.security.allowDomain(domain);
  53.   __gateway = gateway_str;
  54.  }
  55.  public function get gateway():String
  56.  {
  57.   return __gateway;
  58.  }
  59.  
  60.  // parameters to send
  61.  public function set parameters(parameters_obj:Object):Void
  62.  {
  63.   __parameters = parameters_obj;
  64.  }
  65.  public function get parameters():Object
  66.  {
  67.   return __parameters;
  68.  }
  69.  
  70.  // method GET/POST (POST by default)
  71.  public function set method(method_str:String):Void
  72.  {
  73.   __method = method_str;
  74.  }
  75.  public function get method():String
  76.  {
  77.   return __method;
  78.  }
  79.  
  80.  //-----------------------------------------------------------
  81.  // public methods
  82.  //-----------------------------------------------------------
  83.  
  84.  // get the result object
  85.  public function getResult():Object
  86.  {
  87.   return __result;
  88.  }
  89.  
  90.  // get the records array
  91.  public function getRecords():Array
  92.  {
  93.   return getResult().records;
  94.  }
  95.  
  96.  // get a target record
  97.  public function getRecord(index:Number):Object
  98.  {
  99.   return getRecords()[index];
  100.  }
  101.  
  102.  // load method, initiates the SWX loading sequence
  103.  public function loadData():Void
  104.  {
  105.   this.createEmptyMovieClip("__result_mc", 1);
  106.   parseParameters(__parameters)
  107.   __loadcompleted = false;
  108.   __result_mc.loadMovie(__gateway, __method);
  109.   dispatchEvent({type:"onLoadStart", target:this});
  110.   onEnterFrame = checkLoadProgress;
  111.  }
  112.  
  113.  // getPercentageLoaded, returns a value from 0-100 while loading
  114.  public function getPercentageLoaded():Number
  115.  {
  116.   var ld:Number = __result_mc.getBytesLoaded();
  117.   var tt:Number = __result_mc.getBytesTotal();
  118.   if(ld> 4)
  119.   {
  120.    return Math.floor(ld/tt*100);
  121.   }
  122.   else
  123.   {
  124.    return 0;
  125.   }
  126.  }
  127.  
  128.  //-----------------------------------------------------------
  129.  // private methods
  130.  //-----------------------------------------------------------
  131.  
  132.  // checkLoadProgress, applied on enterframe when loading the SWX file
  133.  private function checkLoadProgress():Void
  134.  {
  135.   if(getPercentageLoaded() <100)
  136.   {
  137.    dispatchEvent({type:"onLoadProgress", target:this});
  138.   }
  139.   
  140.   else
  141.   {
  142.    if(__loadcompleted == false)
  143.    {
  144.     __loadcompleted = true;
  145.     dispatchEvent({type:"onLoadComplete", target:this});
  146.    }
  147.    
  148.    if(__loadcompleted && __result_mc.result != undefined)
  149.    {
  150.     onEnterFrame = null;
  151.     
  152.     __result = __result_mc.result;
  153.     dispatchEvent({type:"onLoadInit", target:this});
  154.    }
  155.   }
  156.  }
  157.  
  158.  // push parameters into result_mc
  159.  private function parseParameters(_params:Object):Void
  160.  {
  161.   for(var i in _params)
  162.   {
  163.    __result_mc[i] = _params[i];
  164.   }
  165.   __result_mc.output = "swf";
  166.  }
  167.  
  168.  //-----------------------------------------------------------
  169.  // associate the symbol to the class when the class is defined
  170.  // requirement for dynamically attaching this class as a library item
  171.      static var symbolLinked = Object.registerClass(symbolName, symbolOwner);
  172. }

which inherits from my custom DispatcherMovieClip shown below:

Actionscript:
  1. import mx.events.EventDispatcher;
  2.  
  3. dynamic class nl.onload.utils.DispatcherMovieClip extends MovieClip{
  4.  
  5.  public function DispatcherMovieClip(){
  6.   EventDispatcher.initialize(this);
  7.  }
  8.  
  9.  public function addEventListener(){
  10.  }
  11.  
  12.  public function removeEventListener(){
  13.  }
  14.  
  15.  public function dispatchEvent(){
  16.  }
  17.  
  18.  public function dispatchQueue(){
  19.  }
  20.  
  21. }

This, together with the php file from the previous section should let you integrate swx into your flash projects quite easily once set up.

Hope this is helpful :)

Leave a Reply