Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to send parameters with custom dispatch event

I am creating a library. Here is an example

[Event (name="eventAction", type="something")]
            public function create_new_customer(phone_number:String):void
    {
         -------------;
                     ----;
                     ------------;
          rpc.addEventListener(Event.COMPLETE, onCreate_returns);
    }

    private function onCreate_returns(evt:Event):void
    {
         var ob:Object = evt.target.getResponse();
         dispatchEvent(new something("eventAction"));
    }

I have a listener to this event in app side. So when I manually dispatch event I want the "ob" to be sent as a parameter. How to do it?

like image 622
kp11 Avatar asked Apr 27 '09 06:04

kp11


People also ask

Which method is used to publish your own custom event?

To publish custom events, we will need an instance of ApplicationEventPublisher and then call the method ApplicationEventPublisher#publishEvent(..) . Another way to publish event is to use ApplicationContext#publishEvent(....) .

What does dispatchEvent do in Javascript?

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected EventListener s in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent() .

How do you define a custom event?

Custom event definitions are event definitions that have been created from scratch in the event definition editor rather than having been generated from existing events by the event definition generator.

How do you create an event object?

The createEvent() method creates an event object. The event must be of a legal event type, and must be initialized (dipatched) before use.


1 Answers

You need to create a custom event class with extra properties to pass data with it. In your case you could use a class like

public class YourEvent extends Event
{
    public static const SOMETHING_HAPPENED: String = "somethingHappend";

    public var data: Object;

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

        this.data = data;
    }

    override public function clone():Event
    {
        return new YourEvent (type, data, bubbles, cancelable);
    }

}

then when yo dispatch you do:

dispatchEvent(new YourEvent(YourEvent.SOMETHING_HAPPENED, ob));
like image 104
James Hay Avatar answered Oct 11 '22 12:10

James Hay