views:

48

answers:

1

Hi there,

I am having a hard time wrapping my head around what to do for the following situation:

  • I have multiple items in my app that I would like the user to click on. Some items are completely different classes.

  • The classes dispatch events and then when the events are captured, some modifications are done to the transform properties of a loosely coupled display object.

  • I want two separate classes to dispatch the same type of event, since I already have an image class that draws images based on a given url... I want the classes to dispatch the events, but then I think I want the difference to be detected when the event comes along... i.e., I would like to recognize the 'origin' or 'sub-type' of the event. Some sort of if/then logic would suffice.

Would it be better to have two separate types of events?

How do people normally do this?

Would anyone suggest creating some sort of variable to detect the 'sub-event-type' for the listener of the event?

Thanks for any thoughts...

+2  A: 

The target property of the Event class contains the class that dispatched the event.You can query that property in your event handler to recognize the 'origin' of the event.

Creating a separate (sub)class for the event is another option that would work. I guess it depends on whether it's really the same logical event dispatched by different objects, or logically different events.

Pieter Kuijpers
Thanks very much for this concise reply. Can you elaborate on how one might distinguish one type of target event over the other? Would you usually assign a name to the target and do a check on that?
jml
The target property contains a reference to the actual object that dispatched the event, so you if you know the type of the dispatcher you can cast the target to the proper type and access all its properties (for example some kind of ID if you have it).Doing this introduces some close coupling between the event handler and event dispatcher and I'm not sure this is the best solution in your case. Can you elaborate a bit on what you're trying to achieve?
Pieter Kuijpers
How about having an additional property in the Custom Event class, for the sake of identification? Of course, using 'target' property is also sufficient.
Sai Prasad
Great; thanks a ton!
jml