As I found a solution to my problem, I will post it here, but I leave the question open for some time in case someone come up with a better one.
First I created a CustomEvent
(I could use the CollectionEvent
class but this way I'm sure I don't mess up with the internal logic of the DataGrid
)
package
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static const CHANGE:String = "change";
public var column:String;
public var data:Object;
public function CustomEvent(column:String, data:Object)
{
super(CHANGE, true);
this.column = column;
this.data = data;
}
}
}
Notice the call to super()
with bubbles
set to true. This is actually needed for the event to be handle here.
Then I modified my CheckBoxGridItemRenderer
to dispatch this event when the checkbox is selected/deselected:
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">
<fx:Script>
<![CDATA[
import CustomEvent;
override public function prepare(hasBeenRecycled:Boolean):void {
if (data)
{
checkBox.selected = Boolean(data[column.dataField]);
}
}
private function valueChange():void
{
data[column.dataField] = checkBox.selected;
dispatchEvent(new CustomEvent(column.dataField, data));
}
]]>
</fx:Script>
<s:CheckBox id="checkBox" horizontalCenter="0" change="valueChange()" />
</s:GridItemRenderer>
Finally I only have to listen to my custom event dispatched by the DataGrid
instance:
clients.addEventListener(CustomEvent.CHANGE, handleCustomEvent);