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 the class: import com.gskinner.net.LoadVars2; myLV:LoadVars2 = new LoadVars2(); // subscribe to its load event: myLV.addEventListener("load",this); // add some data to send: myLV.username = "bob"; myLV.password = "555123"; // set the timeout period to 5 seconds (default is 7s): myLV.timeout = 5000; // this should timeout after 5 seconds: myLV.sendAndLoad("http://1.1.1.1/noFile.php",myLV,"POST"); // function to handle the load event: 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 the class: import com.gskinner.net.LoadVars2; myLV:LoadVars2 = new LoadVars2(); // subscribe to its data event: myLV.addEventListener("data",this); // add some data to send: myLV.username = "frank"; myLV.password = "cheesepenguin"; // set the timeout period to 10 seconds (default is 7s): myLV.timeout = 10000; // this should timeout after 10 seconds: myLV.sendAndLoad("http://1.1.1.1/noFile.php",myLV,"POST"); // function to handle the data event: 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.
a little pompous? ultimate loadvars solution?
(grin) – was just following the naming scheme I set with “Ultimate XML solution”
Intentional pomposity, intended humorously. 🙂
Cheers.
AS 2.0: Extending Intrinsic Classes
I’ve been thinking a lot lately about Grant Skinner’s rewrites of the XML and LoadVars classes, mainly to allow for true event broadcasting instead of simply using callbacks. Without talking specifics re the actual implementation, I just want to …
Very useful – thanks
I’m trying to get LoadVars(originally) and now LoadVars2 on a subClass of a form without success. Any hints…
I’ve been having problems with the onTimeOut(). (onData != undefined) is always returning true, so I can’t dispatch a ‘load’ event with success:false to my listener. Any suggestions?
Greetings! We are conducting bet on sports online to all who are interested in playing games. Visit our site for more information and this online game is free for all! So what are you waiting for? Play now and enjoy our site! Happy gaming!
Seems to be a comprehensive tutorial.
Thanks…
A bit hard for me but nice tutorial thank you 🙂
Is there anyway to increase the timeout beyond the 30 seconds?
This script is all i’ve found that comes close to what i need.