Can dispatchEvent() carry arguments in AS3?

Behold this example:

addEventListener("myEventType", myFunction("argument"));

function myFunction(args:String):Function {
    return function(evt:Event):void {
        trace(evt.currentTarget, "has", args);
    };
}

dispatchEvent(new Event("myEventType", true));

It works.

Can I do something similar, but passing "argument" through dispatchEvent()?

It'd be very handy in a situation where dispatchEvent() is in a wholly separated class from addEventListener() and myFunction().

I'll be needing this a lot, so I want to do it without creating a custom event class for every situation.

Answers


You can use native flash.events.DataEvent for passing String parameter or create custom DataEvent with data:* property in all situations where you need to pass parameters to event handler.

If you want to customize the behavior of event listener in the place of adding event listener you can create "listener" object for holding this custom parameters (but I think this technique is more complicated than custom events): addEventListener("myEventType", new EventListener("param1").onEvent);, whereEventListener is the class like this:

public class EventListener
{
    private var params:*;

    public function EventListener(params:*)
    {
            this.params = params;
    }

    public function onEvent(event:Event):void
    {
            trace("onEvent, params = ", params);
    }
}

Need Your Help

Equivalent of editable ComboBox in WinRT?

xaml windows-8 windows-store-apps winrt-xaml

The standard (desktop) windows combobox has three styles; Simple, Dropdown, and Drop List. Dropdown works like an edit control and a combobox, whereas Drop List doesn't allow editing.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.