This may very well be a complete newb question but I’m just now migrating to AS3.
I’m creating a class that evaluates an email address to be valid or not. As part of the process I load a XML-File containing all the top level domain suffixes to determine whether or not the email address has a valid domain.
My problem is that I need to make the class wait to give a return statement until the XML has loaded and been evaluated.
To make it a tad clearer:
The usage:
addressForChecking = new CheckAddress("me@mydomain.com");
var isValid:Boolean = addressForChecking.checkIt();
trace("The result is: " + isValid)
The class:
package no.martinjacobsen.Utils {
import flash.events.*;
import flash.net.*;
public class CheckAddress {
private var XMLDomains:XML;
private var XMLLocation:String = "http://www.hastalasiesta.org/stuffs/topleveldomains.xml";
private var urlLoader:URLLoader;
private var theAddress:String;
private var lastDot:Number;
private var restOfAddress:Number;
private var domainName:String;
private var theWrong:String;
public function CheckAddress(addressToCheck:String){
theAddress = addressToCheck;
lastDot = theAddress.lastIndexOf(".");
restOfAddress = theAddress.length;
domainName = theAddress.substr(lastDot,restOfAddress);
}
public function checkIt(): Boolean {
var isItValid:Boolean;
var urlRequest:URLRequest = new URLRequest(XMLLocation);
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeListener);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorListener);
urlLoader.load(urlRequest);
/* HERE I NEED TO SOMEHOW DELAY BEFORE EVALUATING THE ADDRESS */
if(suffixChecker(domainName) && atIsNotFirst(theAddress)){
return true;
} else {
return false;
}
}
private function errorListener (e:Event) : Boolean {
/* ERROR MESSAGE SKIPPED FOR BREVITY */
}
private function completeListener (e:Event) : Boolean{
/* CAN I SOMEHOW TRIGGER THE EVALUATION CODE HERE
* YET STILL MAKE checkIt() RETURN THE VALUE? */
}
private function atIsNotFirst(atAddy:String):Boolean{
/* CODE SKIPPED FOR BREVITY */
}
private function suffixChecker (domainName:String):Boolean {
/* CODE SKIPPED FOR BREVITY */
}
}
}
So; I need the XML-file to load before the checkIt() function returns it’s Boolean, but I have no clue how to make checkIt() wait for the eventListener to do it’s stuff before leaping into action (or indeed if it’s possible).
Was that understandable?