Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I code MXML to attach a listener to a custom ActionScript class

For example, the following MXML script attaches a listener to a Button class:

<mx:Button id="STACK" label="OVERFLOW" click="doStuff()"/>

I have a custom action script class which fires an event when a value is updated and I wish to be able to listen for that event in an MXML class:

ActionScript Class:

public function set currentPage(newCurrentPage: Number) : void {
    _currentPage = newCurrentPage;

    dispatchEvent(new DataEvent(PAGE_CHANGED, true, false, _currentPage));
}

And I wish to be able to do the following in MXML:

<myClass:Class <...> pageChanged="doMoreStuff()" />

How would I do this? Cheers :)

like image 657
Richard Walton Avatar asked Feb 26 '09 11:02

Richard Walton


1 Answers

You have to declare the event with a metadata tag:

<mx:Metadata>
[Event(name="pageChanged", type="full.type.name.of.DataEvent")]
</mx:Metadata>

The name of the event must match the event name (PAGE_CHANGED constant in your example).

Edit: if you're writing the class in ActionScript instead of MXML, you can apply the metadata tag directly to your class:

[Event(name="pageChanged", type="full.type.name.of.DataEvent")]
public class MyClass extends WhateverItExtends
like image 174
Rytmis Avatar answered Oct 04 '22 01:10

Rytmis