In MXML you cannot evaluate constants as attribute names so

 

viewChanged=

 

is required

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of bpjim
Sent: Wednesday, February 06, 2008 8:51 AM
To: [email protected]
Subject: [flexcoders] Differences between MXML and ActionScript in
setting event listeners?

 

I have a custom component (PlotControls.mxml). It inherits from
DisplayObject (via HBox), so my understanding is that it will support
event bubbling.

In it I have defined a 'viewChanged' event:

<mx:Metadata>
[Event(name="VIEW_CHANGED", type="flash.events.Event")]
</mx:Metadata>

public static const VIEW_CHANGED:String = "viewChanged";

In that component I also have some AS code which broadcasts that event
(notice that bubbling is TRUE):

private function viewSelectedHandler():void
{
dispatchEvent(new Event(PlotControls.VIEW_CHANGED, true));
}

-------------
Next, I have a parent component, which also inherits from
DisplayObject via HBox. In the parent component, I have created an
instance of the first (child) component:

<timeSeriesPlots:PlotControls id="plotControls"
plotViews="{AppData.getInstance().selectedExpTimeSeriesViews}" />

I also have some initializer code in the parent component which adds
an event handler to the child component:

private function initComponent():void
{
plotControls.addEventListener(PlotControls.VIEW_CHANGED,
viewSelectedHandler);
}

And the handler code looks like this:

private function viewSelectedHandler(e:Event):void
{
var plotId:String = plotControls.getPlotId();
Alert.show('viewSelectedHandler(); plotId: '+plotId);
}

All of the code ABOVE works as expected: when the UI in the child
component dispatches the event, the parent component's event handler
is getting called.

-------------
On the other hand, I tried setting the event listener directly on the
child component itself via the MXML markup (and removed the
addListener call from the parent's initializer):

<timeSeriesPlots:PlotControls id="plotControls"
plotViews="{AppData.getInstance().selectedExpTimeSeriesViews}"
VIEW_CHANGED="viewSelectedHandler(event);"
/>

Note: the Flex Builder IDE is showing me 'code completion' information
for the VIEW_CHANGED event, so I think that I have the right syntax
here, at least.

BUT THIS DOESN'T WORK! (The event handler is never invoked.)

*** My Question: why does the event handler, viewSelectedHandler(),
for the VIEW_CHANGED event NEVER get called when I add the listener
directly via the MXML markup? My understanding of Flex is that these
two approaches should yield the same results (though using
addListener() gives you more options.

What am I overlooking here--this one has me stumped. Could someone
please give me a clue?

Thanks in advance,
Jim

 

Reply via email to