1

I saw a question just like this, but the answer avoids the point with a callback function from the inside of class.

I did a class that reads a directory (ListaDir), make the request to a php server-side and process the answer. I wish to use like this:

var Lista:ListaDir = new ListaDir(urltophplibrarythatreadthedirectory); // this is working
Lista.addEventListener(Event.COMPLETE, myHandlerFunc); // this is the dream

function myHandlerFunc(e){
var Res:Object = e.target.data; // The answer in object containing the list and another info
}

Can be done like this? How?

1 Answer 1

3

Have your custom class extend EventDispatcher and dispatch the desired event.

For example, for simple events like Event.Complete simply dispatch a new Event:

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class ListaDir extends EventDispatcher
    {
        public function dispatch():void
        {
            dispatchEvent(new Event(Event.COMPLETE));
        }
    }
}

If you need to dispatch an event with data, it may be optimal to create your event classes.

CustomEvent class

As an example, this CustomEvent demonstrates a data payload object:

package
{
    import flash.events.Event;

    public class CustomEvent extends Event
    {
        public static const COMPLETE:String = "COMPLETE";

        public var data:*;

        public function CustomEvent(type:String, data:*, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);

            if (data)
                this.data = data;
        }
    }
}

ListaDir class

When this class dispatches your CustomEvent, it can send a payload of data:

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class ListaDir extends EventDispatcher
    {
        public function dispatch():void
        {
            var dataObject:Object = {name: "Example Data"};
            dispatchEvent(new CustomEvent(CustomEvent.COMPLETE, dataObject));
        }
    }
}

Implementation:

Instantiate and create an event listener:

var listaDir:ListaDir = new ListaDir();
listaDir.addEventListener(CustomEvent.COMPLETE, completeHandler);

On complete, retrieve your data object:

protected function completeHandler(event:CustomEvent):void
{
    var dataObject:* = event.data;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Cool! But I'm getting the error "1180: Call to a possibly undefined method dispatchEvent". Is there some library missing to import?
Make sure your ListaDir class extends EventDispatcher as in the example(s) above.
It works!!! Thanks! I think I just did a huge footstep ahed, I'm just starting to understand a few of as3 and object-oriented programming...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.