[flexcoders] how to bubble and catch events

2009-02-13 Thread stevepruitt97
I have the following scenario.  I need to catch the click events for 
either of the two buttons.  I tried putting the event handler on the 
DataGrid, but I'm getting an unresolved error for it.  Is this even 
possible?

Thanks in advance for any help.

-S

mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml;
mx:Script
![CDATA[
import mx.collections.ArrayCollection;
import 
mx.controls.dataGridClasses.DataGridItemRenderer;

[Bindable]
private var _content : ArrayCollection = new 
ArrayCollection();

:
:

private function onAction(event : Event) : void
{
trace(got action:  + event.target.id);
}
]]
/mx:Script
mx:DataGrid id=templateGrid  width=100% height=100% 
rowCount=5 dataProvider={_content} action=onAction(event)
mx:columns
mx:DataGridColumn id=actionColumn headerText=Action
mx:itemRenderer
mx:Component
mx:ViewStack
mx:Metadata
[Event(name = action, type 
= flash.events.Event)]
/mx:Metadata
mx:Script
![CDATA[
private function onAction(event : 
Event) : void
{
trace(action:  + event.target.id);
var e : Event;

if(event.target.id == adder)
e = new Event(Event.ADDED, 
true);
if(event.target.id == deleter)
e = new Event(Event.REMOVED, 
true);

dispatchEvent(e);
}
]]
/mx:Script
mx:Text text=/
mx:Button id=adder label=Add 
click=onAction(event)/
mx:Button id=deleter label=Delete 
click=onAction(event)/
/mx:ViewStack 
/mx:Component
/mx:itemRenderer
/mx:DataGridColumn
!--mx:DataGridColumn dataField=index headerText=ID/--

mx:DataGridColumn dataField=title 
headerText=Template/
mx:DataGridColumn dataField=description 
headerText=Description/
mx:DataGridColumn dataField=version 
headerText=Version/
/mx:columns
/mx:DataGrid
/mx:Canvas



RE: [flexcoders] how to bubble and catch events

2009-02-13 Thread Tracy Spratt
That should work, the second argument in the event constructor makes it
bubble.

 

I don't see your listener defined.  You should do that in a function
called on creation complete of the application:

this.addEventListener(Event.ADDED, onAction)

 

Tracy Spratt 
Lariat Services 

Flex development bandwidth available 



From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On
Behalf Of stevepruitt97
Sent: Friday, February 13, 2009 12:08 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] how to bubble and catch events

 

I have the following scenario. I need to catch the click events for 
either of the two buttons. I tried putting the event handler on the 
DataGrid, but I'm getting an unresolved error for it. Is this even 
possible?

Thanks in advance for any help.

-S

mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
http://www.adobe.com/2006/mxml 
mx:Script
![CDATA[
import mx.collections.ArrayCollection;
import 
mx.controls.dataGridClasses.DataGridItemRenderer;

[Bindable]
private var _content : ArrayCollection = new 
ArrayCollection();

:
:

private function onAction(event : Event) : void
{
trace(got action:  + event.target.id);
}
]]
/mx:Script
mx:DataGrid id=templateGrid width=100% height=100% 
rowCount=5 dataProvider={_content} action=onAction(event)
mx:columns
mx:DataGridColumn id=actionColumn headerText=Action
mx:itemRenderer
mx:Component
mx:ViewStack
mx:Metadata
[Event(name = action, type 
= flash.events.Event)]
/mx:Metadata
mx:Script
![CDATA[
private function onAction(event : 
Event) : void
{
trace(action:  + event.target.id);
var e : Event;

if(event.target.id == adder)
e = new Event(Event.ADDED, 
true);
if(event.target.id == deleter)
e = new Event(Event.REMOVED, 
true);

dispatchEvent(e);
}
]]
/mx:Script
mx:Text text=/
mx:Button id=adder label=Add 
click=onAction(event)/
mx:Button id=deleter label=Delete 
click=onAction(event)/
/mx:ViewStack 
/mx:Component
/mx:itemRenderer
/mx:DataGridColumn
!--mx:DataGridColumn dataField=index headerText=ID/--

mx:DataGridColumn dataField=title 
headerText=Template/
mx:DataGridColumn dataField=description 
headerText=Description/
mx:DataGridColumn dataField=version 
headerText=Version/
/mx:columns
/mx:DataGrid
/mx:Canvas