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:
-
// import the SWXVars class
-
import nl.onload.phpflash.SWXVars;
-
-
// setup eventlisteners
-
function onLoadInit(evtObj:Object)
-
{
-
var record = evtObj.target.getRecord(0);
-
trace(record.ID +":"+record.post_title);
-
}
-
-
// create a new SWXVars instance
-
swx = SWXVars.createInstance(this, "swx_mc", 1);
-
-
// setup parameters
-
swx.gateway = "http://localhost/flashphp/php/gateway.php";
-
swx.parameters = {service:"getPosts", page:1, perpage:1};
-
swx.method = "GET";
-
-
// add listeners
-
swx.addEventListener("onLoadInit", this);
-
-
// load data
-
swx.loadData();
The above should illustrate the usage of the SWXVars class. The Class itself is descibed below:
-
// allows for dispatching events
-
import nl.onload.utils.DispatcherMovieClip;
-
-
dynamic class nl.onload.phpflash.SWXVars extends DispatcherMovieClip
-
{
-
-
//-----------------------------------------------------------
-
// variables
-
//-----------------------------------------------------------
-
-
// requirement for dynamically creating the libraryitem
-
// via http://www.peterjoel.com/blog/?archive=2004_01_01_archive.xml
-
static var symbolName:String = "__Packages.nl.onload.phpflash.SWXVars";
-
static var symbolOwner:Function = SWXVars;
-
-
// for debugging purposes
-
private var className:String = "nl.onload.phpflash.SWXVars";
-
-
// private variables accessible via getters/setters
-
private var __gateway:String;
-
private var __method:String = "POST";
-
private var __parameters:Object;
-
private var __result_mc:MovieClip;
-
private var __result:Object;
-
private var __loadcompleted:Boolean = false;
-
-
//-----------------------------------------------------------
-
// class methods
-
//-----------------------------------------------------------
-
-
// constructor
-
function SWXVars()
-
{
-
}
-
-
// static function createInstance to create a new SWXVars instance
-
static function createInstance(path, instanceName, depth, initObj):SWXVars
-
{
-
path.attachMovie(symbolName, instanceName, depth, initObj);
-
var SWXVarsInstance:SWXVars = path[instanceName];
-
return SWXVarsInstance;
-
}
-
-
//-----------------------------------------------------------
-
// getters/setters
-
//-----------------------------------------------------------
-
-
// gateway path
-
public function set gateway(gateway_str:String):Void
-
{
-
var domain:String = gateway_str.split("/").slice(0,3).join("/");
-
System.security.allowDomain(domain);
-
__gateway = gateway_str;
-
}
-
public function get gateway():String
-
{
-
return __gateway;
-
}
-
-
// parameters to send
-
public function set parameters(parameters_obj:Object):Void
-
{
-
__parameters = parameters_obj;
-
}
-
public function get parameters():Object
-
{
-
return __parameters;
-
}
-
-
// method GET/POST (POST by default)
-
public function set method(method_str:String):Void
-
{
-
__method = method_str;
-
}
-
public function get method():String
-
{
-
return __method;
-
}
-
-
//-----------------------------------------------------------
-
// public methods
-
//-----------------------------------------------------------
-
-
// get the result object
-
public function getResult():Object
-
{
-
return __result;
-
}
-
-
// get the records array
-
public function getRecords():Array
-
{
-
return getResult().records;
-
}
-
-
// get a target record
-
public function getRecord(index:Number):Object
-
{
-
return getRecords()[index];
-
}
-
-
// load method, initiates the SWX loading sequence
-
public function loadData():Void
-
{
-
this.createEmptyMovieClip("__result_mc", 1);
-
parseParameters(__parameters)
-
__loadcompleted = false;
-
__result_mc.loadMovie(__gateway, __method);
-
dispatchEvent({type:"onLoadStart", target:this});
-
onEnterFrame = checkLoadProgress;
-
}
-
-
// getPercentageLoaded, returns a value from 0-100 while loading
-
public function getPercentageLoaded():Number
-
{
-
var ld:Number = __result_mc.getBytesLoaded();
-
var tt:Number = __result_mc.getBytesTotal();
-
if(ld> 4)
-
{
-
return Math.floor(ld/tt*100);
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
//-----------------------------------------------------------
-
// private methods
-
//-----------------------------------------------------------
-
-
// checkLoadProgress, applied on enterframe when loading the SWX file
-
private function checkLoadProgress():Void
-
{
-
if(getPercentageLoaded() <100)
-
{
-
dispatchEvent({type:"onLoadProgress", target:this});
-
}
-
-
else
-
{
-
if(__loadcompleted == false)
-
{
-
__loadcompleted = true;
-
dispatchEvent({type:"onLoadComplete", target:this});
-
}
-
-
if(__loadcompleted && __result_mc.result != undefined)
-
{
-
onEnterFrame = null;
-
-
__result = __result_mc.result;
-
dispatchEvent({type:"onLoadInit", target:this});
-
}
-
}
-
}
-
-
// push parameters into result_mc
-
private function parseParameters(_params:Object):Void
-
{
-
for(var i in _params)
-
{
-
__result_mc[i] = _params[i];
-
}
-
__result_mc.output = "swf";
-
}
-
-
//-----------------------------------------------------------
-
// associate the symbol to the class when the class is defined
-
// requirement for dynamically attaching this class as a library item
-
static var symbolLinked = Object.registerClass(symbolName, symbolOwner);
-
}
which inherits from my custom DispatcherMovieClip shown below:
-
import mx.events.EventDispatcher;
-
-
dynamic class nl.onload.utils.DispatcherMovieClip extends MovieClip{
-
-
public function DispatcherMovieClip(){
-
EventDispatcher.initialize(this);
-
}
-
-
public function addEventListener(){
-
}
-
-
public function removeEventListener(){
-
}
-
-
public function dispatchEvent(){
-
}
-
-
public function dispatchQueue(){
-
}
-
-
}
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 ![]()
