如何仅在分配参数变量时才运行函数?

发布于 2024-10-19 22:50:56 字数 894 浏览 1 评论 0原文

我需要一种方法来等待运行 parseCSV 命令,直到 readFile 事件更新了 importData 的内容。我已经了解了一些有关自定义事件调度程序的内容,但无法完全弄清楚如何在我的情况下使用它们。

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();
    }

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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-10-26 22:50:56

您需要添加一些回调或添加一些事件侦听器。我更喜欢回调:

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);
}

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);
}
冰魂雪魄 2024-10-26 22:50:56

只在 readFile 函数中添加这一行怎么样?

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();
}

一旦设置了 importData,该命令就会被执行。

如果您希望自定义事件路由,则需要调度您自己的自定义事件。每个事件都有一个类型参数,它只是一个用于标识它的字符串。例如,Event.CHANGE 与使用“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");
}

所以你可以尝试这样的事情。

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文