Monday, May 10, 2010

Add Events to Interfaces

Using the Event tag allows you to have events in Interfaces. With intellisense it will show a list of valid events when a method expects an event as a parameter, like the EventDispatcher::addEventListener method. They have to map exactly to each other.

[Event(name = "concrete string representation of event", type = "namespace to class with concrete instantiation")]

Comments in the interface override the comments in the concrete class when using the interface. Although you can specify the tag in the class it is customary to specify them after import declarations and before the class declaration.

Example:

Interface File:
package com.site
{
  import flash.events.IEventDispatcher;

  [Event(name = "some_event", type = "com.site.Do")]

  public interface IDo extends IEventDispatcher
  {
    function doSomething():Boolean;
  }
}

Class File:
package com.site
{
  import flash.events.EventDispatcher;

  public class Do extends EventDispatcher implements IDo
  {
    static public const SOME_EVENT:String = "some_event";
    public function doSomething():Boolean{ return true;}
  }
}

Now when we use a method that expects an event; intellisense will display the options if you are using the interface.

No comments: