监听自定义事件时要添加什么事件监听器?

发布于 2024-10-22 08:51:23 字数 708 浏览 6 评论 0原文

当 sql 查询返回不匹配项时,我发送一个事件,以便我可以继续添加到数据库。看起来动作脚本要求我将侦听器附加到某些东西,但我实际上没有任何看起来像的变量就像我在代码中的逻辑候选人一样。

我只想监听 isNewRecord 事件被调用,以便我可以运行插入查询;现在它说调用 addEventListern 和dispatchEvent 可能未定义的方法

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void{
        checkRecord('Gb', datetime, matchRecord);

        addEventListener("isNewRecord", recordExists);

        function recordExists()
        {/*code to execute query*/}

public function matchRecord(result:SQLResult):void {
        var match:String = result.data[0];
        if (match == null) {
            var allClear:Event = new Event("isNewRecord");
            dispatchEvent(allClear);
        }
    }

I am sending an event when an sql query returns no matches so that I can continue with the addition to the database.. It seems like actionscript is requiring that I attach the listener to something, but I don't really have any variables that seem like logical candidates at the point where I am in my code.

I just want to listen for the isNewRecord event to be called so that I can then run the insert query; right now it's saying call to possibly undefined method for addEventListern and for dispatchEvent

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void{
        checkRecord('Gb', datetime, matchRecord);

        addEventListener("isNewRecord", recordExists);

        function recordExists()
        {/*code to execute query*/}

public function matchRecord(result:SQLResult):void {
        var match:String = result.data[0];
        if (match == null) {
            var allClear:Event = new Event("isNewRecord");
            dispatchEvent(allClear);
        }
    }

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

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

发布评论

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

评论(2

猫弦 2024-10-29 08:51:23

你的代码有问题。你有一个函数中的函数。

另外,您的代码是否扩展了 EventDispatcher 类(或扩展它的任何类,如 Sprite、MovieClip 等?)请确保它是。

试试这个:

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void
{
        // note, you're adding this event listener EVERY TIME you call the 
        // addBG function, so make sure you remove it OR add it somewhere in the
        // init or complete functions

        addEventListener("isNewRecord", recordExists);
        checkRecord('Gb', datetime, matchRecord);    
}
public function recordExists():void
{/*code to execute query*/}

public function matchRecord(result:SQLResult):void {
        var match:String = result.data[0];
        if (match == null) {
            var allClear:Event = new Event("isNewRecord");
            dispatchEvent(allClear);
        }
}

Your code is buggy. You have a function within a function.

Also, is your code extending EventDispatcher class (or any class that extends it, like Sprite, MovieClip, etc.?) Make sure it is.

Try this:

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void
{
        // note, you're adding this event listener EVERY TIME you call the 
        // addBG function, so make sure you remove it OR add it somewhere in the
        // init or complete functions

        addEventListener("isNewRecord", recordExists);
        checkRecord('Gb', datetime, matchRecord);    
}
public function recordExists():void
{/*code to execute query*/}

public function matchRecord(result:SQLResult):void {
        var match:String = result.data[0];
        if (match == null) {
            var allClear:Event = new Event("isNewRecord");
            dispatchEvent(allClear);
        }
}
鼻尖触碰 2024-10-29 08:51:23

您不需要使用事件。您对 SQLResult 的处理似乎是同步的,没有由于与用户、服务器或任何可能需要一些时间的交互而导致的延迟。

当 Flash 执行您的代码时,它会执行以下操作:

checkRecord('Gb', datetime, matchRecord);
//then
var match:String = result.data[0];
if (match == null) {
    var allClear:Event = new Event("isNewRecord");
    dispatchEvent(allClear);
}
//and finally
addEventListener("isNewRecord", recordExists);

在添加侦听器之前调度事件。
这是你应该做的:

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void
{
        if (checkRecord('Gb', datetime, matchRecord))
        {
            recordExists();
        }
}

public function recordExists():void
{/*code to execute query*/}

public function matchRecord(result:SQLResult):Boolean{
        var match:String = result.data[0];
        if (match == null) {
            return true;
        }
        return false;
}

干杯

You don't need to use events. Your processing of the SQLResult seems synchronous, there is no latency due to any interaction with the user, the server or anything that may take some time.

When Flash executes your code it does the folowing:

checkRecord('Gb', datetime, matchRecord);
//then
var match:String = result.data[0];
if (match == null) {
    var allClear:Event = new Event("isNewRecord");
    dispatchEvent(allClear);
}
//and finally
addEventListener("isNewRecord", recordExists);

The event is dispatched before the listener is added.
Here is what you should do:

public function addBG(BG:Number, datetime:String, batch:Boolean = false):void
{
        if (checkRecord('Gb', datetime, matchRecord))
        {
            recordExists();
        }
}

public function recordExists():void
{/*code to execute query*/}

public function matchRecord(result:SQLResult):Boolean{
        var match:String = result.data[0];
        if (match == null) {
            return true;
        }
        return false;
}

Cheers

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