Question

I need a way to wait running the parseCSV command until the readFile event has updated the content of importData. I have seen a few things about custom event dispatchers but cannot quite figure out how to use them in my situation.

private var importData : String;

    public function importFile(event:MouseEvent):void {
        var data:String = chooseFile();
        parseCSV(importData);
    }

    public function chooseFile ():String {
        var filetype:FileFilter = new FileFilter("CSV Files(*.csv)","*.csv");
        var file:File = File.userDirectory;
        file.browseForOpen("Select CSV file to import", [filetype]);
        file.addEventListener(Event.SELECT, readFile);
        return importData;
    }

public function readFile (event:Event):void {
        var filestream:FileStream = new FileStream();
        filestream.open(event.target as File, FileMode.READ);
        importData = filestream.readUTFBytes(filestream.bytesAvailable);
        filestream.close();
    }
Was it helpful?

Solution

You'll either need to add some callbacks or add some event listeners. I prefer callbacks:

function importFile(...) {
     choseFile(function(file:File) {
         readFile(file, parseCSV);
     });
}

function choseFile(callback:Function) {
    ...
    file.addEventListener(Event.SELECT, function(event:Event) {
        callback(File(event.target));
    });
}

function readFile(file:File, callback:Function) {
    var data = ... read data from file ....;
    callback(data);
}

OTHER TIPS

What about just adding the line in the readFile function?

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    parseCSV(importData);
    filestream.close();
}

The command will be executed as soon as importData is set.

If you wish to the custom events route, you need to dispatch your own custom Event. Each Event has a type parameter which is just a string to identify it with. For example Event.CHANGE is the same as using "change".

static public const CUSTOM = "myCustomEvent";

public function someConstructor():void {
    addEventListener(CUSTOM, onCustomEvent);
}

public function testDispatch():void{
    dispatchEvent(new Event(CUSTOM));
}

private function onCustomEvent(e:Event):void{
    trace("custom event Dispatched");
}

So you could try something like this.

public function importFile(event:MouseEvent):void {
        addEventListener(CUSTOM, onImport);
        var data:String = chooseFile();
    }

private function onImport(e:Event):void {
    parseCSV(importData);
}

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    dispatchEvent(new Event(CUSTOM));
    filestream.close();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top