Hello. I have a new blog.

I've moved this blog to the following URL Kerkness.ca. Thank you for visiting, please update your bookmarks.

Wednesday, March 12, 2008

Flex Tip: Adding custom events to Actionscript components

One of the key features of building a Flex application with custom components is being able to define and dispatch custom events. In order to do this you need to add some metadata to your custom component.

If you're creating an actionscript class for your custom component you declare the metadata before defining your class as in the following example.

package claire.libs
{
....
[Event(name="serviceError", type="flash.events.Event")]
public class ClaireService extends HTTPService
{
.....
}
}

You will now be able to add an event listener in MXML with the following.
<mycom:ClaireService id="myService" serviceError="handlerFunction(event)"/>

While the above example works it is important to note that the following example will NOT work. Notice the ";" at the end of the Event metatag. Took me about 2 hours to figure this out so hopefully this post will save someone else some time.
package claire.libs
{
....
[Event(name="serviceError", type="flash.events.Event")];
public class ClaireService extends HTTPService
{
.....
}
}

No comments:

Post a Comment

Thank you for the comments.