gskinner.com: gBlog: FlashMX 2004 source code, news, reviews and opinions
Back to gBlog Main gskinner.com website: source code, portfolio, etc. contact Grant Skinner

Syndication
RSS 1.0
RSS 2.0


Subscribe
Enter your email address to be notified when posts are added.

Search

Resources
Conference Session Notes

Flash Blogs
Waxpraxis
Philterblog
W3Blog
Jonas Galvez
Josh Dura
Quasimondo
Flashguru
Sean Voisen
Colin Moock
Flazoom
Greg Burch
Pope De Flash
Peter Hall
Glyn Thomas
actionscript.com
Princess Pegg


Aggregators
Flog
FullAsAGoog
MXNA
Hall of Justhese


« 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 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.

Posted @ 11:27 AM by Grant | TrackBack


Comments

a little pompous? ultimate loadvars solution?

Posted by: Nick Vent at March 16, 2004 03:26 PM

(grin) - was just following the naming scheme I set with "Ultimate XML solution"

Intentional pomposity, intended humorously. :)

Cheers.

Posted by: Grant Skinner at March 16, 2004 03:33 PM

Very useful - thanks

Posted by: Jay Silva at August 2, 2004 08:46 AM


Hosting by NetKeepers.ca | Powered by Movable Type 2.661
The text content of this blog is licensed under a Creative Commons License. Graphics are ©2003 Grant Skinner.