 |
|
 |
« EULA update: Close, but no cigar |
Back to Main
| EULA Update Discussion »
 |
March 16, 2004 |
 |
|
IMPORTANT NOTE: This is an old archive. It is only here to support outdated external links. To view the updated version of this archive, please go to the blog index, and search for the title of this document using the search form.
|
The ultimate AS2 LoadVars solution.
Posted by Grant
Having extended XML into an ActionScript 2.0 friendly class (see: The ultimate AS2 XML solution!), I didn't feel it was appropriate to neglect poor old LoadVars. Sure LoadVars is getting a little old and rusty, but its still useful at times, and it sure would be nice to have it in an AS2 friendly format.
So I toiled away, and the end result is LoadVars2, which supports EventDispatcher events, and configurable timeout periods....
Here's what LoadVars2 looks like in use:
import com.gskinner.net.LoadVars2;
myLV:LoadVars2 = new LoadVars2();
myLV.addEventListener("load",this);
myLV.username = "bob";
myLV.password = "555123";
myLV.timeout = 5000;
myLV.sendAndLoad("http://1.1.1.1/noFile.php",myLV,"POST");
function load(p_evtObj:Object):Void {
if (p_evtObj.success) {
trace("Loaded successfully");
} else {
trace("An error occurred while loading");
}
}
As you can see in the example above, the success flag is passed back as a property of the event object (p_evtObj).
You can also subscribe to the "data" event. Just like LoadVars onData, this event will fire before the loaded data is parsed, allowing you to access the raw text source of the return. Also just like onData, subscribing to the "data" event has the effect of disabling the "load" event - you can subscribe to one or the other, but not both.
import com.gskinner.net.LoadVars2;
myLV:LoadVars2 = new LoadVars2();
myLV.addEventListener("data",this);
myLV.username = "frank";
myLV.password = "cheesepenguin";
myLV.timeout = 10000;
myLV.sendAndLoad("http://1.1.1.1/noFile.php",myLV,"POST");
function data(p_evtObj:Object):Void {
if (p_evtObj.src != undefined) {
trace("Loaded successfully with source: "+p_evtObj.src);
} else {
trace("An error occurred while loading");
}
}
The raw source returned by a "data" event is returned in the "src" property of the event object (p_evtObj). Just like onData, a failed load (including timeouts) will return undefined as the "src" property.
I hope this proves useful to someone. To download the class (and a really simple example file - check the source, because there are no visual elements), click here. I won't claim to have tested this extensively, so please let me know if you experience any issues with it in the comments below.
I also want to give a quick nod to Soren Jepsen who sent me a LoadVars2 implementation of his own. I may implement a few of his ideas in a future version of LoadVars2.
Posted @ 11:27 AM by Grant
| TrackBack
Comments
a little pompous? ultimate loadvars solution?
(grin) - was just following the naming scheme I set with "Ultimate XML solution"
Intentional pomposity, intended humorously. :)
Cheers.
Very useful - thanks
|
 |