开发者

what to addEventListener to when listening for custom events?

开发者 https://www.devze.com 2023-02-17 06:08 出处:网络
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 开发者_JAVA技巧listener

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 开发者_JAVA技巧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);
        }
    }


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消