# AS3 Custom Event best practices

**URL:** <https://forum.kirupa.com/t/as3-custom-event-best-practices/633586>\
**Category:** flash\
**Created:** [August 5, 2015, 12:59pm UTC](https://forum.kirupa.com/t/as3-custom-event-best-practices/633586 "2015-08-05T12:59:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamcknig](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kamcknig/32/7342_2.png) [@kamcknig](https://forum.kirupa.com/u/kamcknig)\
**Post date:** [August 5, 2015, 12:59pm UTC](https://forum.kirupa.com/t/as3-custom-event-best-practices/633586/1 "2015-08-05T12:59:45Z")

</div>

I’m just curious how a lot of you handle custom events.

My custom event/s that I will be creating pertain to creating and displaying views. I want to have two separate events dispatched, one to create the view and then one to display the view.

I see three ways of doing this:

1. Create a single ViewEvent class with two constants CREATE\_VIEW and DISPLAY\_VIEW. The problem with a single event class is that I would like if possible to use strongly typed parameters. WHen using CREATE\_VIEW though, I want to pass an ID that corresponds to the view to create and when I dispatch DISPLAY\_VIEW I’d like to pass the view object to display. So I would need to either use two optional parameters, one for viewId of type int and one for view of type DisplayObject and then test for which one exists which I don’t like.

2. Same as number 1 except use a single generic parameter and check for it’s type to see what the event’s payload is which I don’t like generic parameters either.

3. Create two separate event classes, a CreateViewEvent and a DisplayViewEvent. This would allow me to use the proper typed parameters, but it seems a little weird since both event actions are so closely related that I feel they should be within the same event class.

So the question is… in general what are everyone’s views on handling a situation like this?

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 11, 2015, 3:31am UTC](https://forum.kirupa.com/t/as3-custom-event-best-practices/633586/2 "2015-08-11T03:31:13Z")

</div>

Hey kamcknig

I don’t know the exact purpose with why you have 2 separate events, but personally I would go with option 1. I definitely would not pass a generic argument as in option 2.

My decision is based on the purpose of the class, which in this case is to manage all the view events: Creating, displaying, removing, updating, sorting, etc.

Do both CREATE\_VIEW and DISPLAY\_VIEW events occur simultaneously? Have you considered doing it sequentially instead? So after you pass the viewId and the view is created, only then pass the DisplayObject?

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [August 11, 2015, 3:14pm UTC](https://forum.kirupa.com/t/as3-custom-event-best-practices/633586/3 "2015-08-11T15:14:12Z")

</div>

With option 1, presumably you’d have a separate handler for each event and would therefore know which event you are handling. Thus you wouldn’t need to check if the property exists. For example:

function handleCreateView(e:ViewEvent):void { trace(e.viewID) }  
function handleDisplayView(e:ViewEvent):void {trace(e.view) }
