How to stop itemRenderer element with first datagrid row from instance / add / initialize / etc twice?

On the first line, the Flex DataGrid ItemRenderer will be initialized twice. Tracing the results shows that the wireframe probably creates two instances of the first itemRenderer. In a more complex application, where itemRenderer contains a data-bound ColorPicker, we see that an endless loop occurs due to this problem. Only the first itemRenderer is initialized twice. Is there a way to overcome flexible behavior and stop it? The following code demonstrates the problem:

Main application:

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="on_initialize(event);"> <mx:Script> <![CDATA[ /** * This experiment shows how the first row itemrenderer is instantiated/added/initialized twice. * We've never even noticed this before we found that a data-bound ColorPicker enters a infinite * loop when it is within an itemRenderer. */ import mx.collections.ArrayCollection; import mx.events.FlexEvent; private var dg_array:Array; private var dg_arrayCollection:ArrayCollection; private function on_initialize(event:FlexEvent):void { dg_array = new Array(); dg_arrayCollection = new ArrayCollection(); dg_arrayCollection.addItem("item 1"); dg_arrayCollection.addItem("item 2"); dg.dataProvider = dg_arrayCollection; } ]]> </mx:Script> <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5"> <mx:columns> <mx:DataGridColumn headerText="Name" itemRenderer="SimpleItemRenderer"/> </mx:columns> </mx:DataGrid> </mx:Application> 

SimpleItemRenderer:

 <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" initialize="//on_initialize(event);"> <mx:Script> <![CDATA[ import mx.events.FlexEvent; [Bindable] override public function set data(value:Object):void { _data = value; } override public function get data():Object { return _data; } private var _data:Object; private function on_initialize_textInput(event:FlexEvent):void { trace("initialize:event.target="+event.target+", " + _data); // runs twice, for the first item only } private function on_creationComplete_textInput(event:FlexEvent):void { trace("creationComplete:event.target="+event.target+", " + _data); // runs twice, for the first item only } ]]> </mx:Script> <mx:TextInput text="{data}" id="textInput" initialize="on_initialize_textInput(event);" creationComplete="on_creationComplete_textInput(event);"/> </mx:Canvas> 

Abbreviated output:

initialize: event.target = ItemRenderers0.dg ... SimpleItemRenderer12.textInput, null initialize: event.target = ItemRenderers0.dg ... SimpleItemRenderer24.textInput, null createComplete: event.target = ItemRenderers0.dg ... SimpleItemRenter item 1 initialize: event.target = ItemRenderers0.dg ... SimpleItemRenderer29.textInput, null createComplete: event.target = ItemRenderers0.dg ... SimpleItemRenderer29.textInput, item 2 createComplete: event.target = ItemRenderers0.dg ... SimpleItem .textInput, item 1

+4
source share
3 answers

Your itemRenderer is really not performing properly, and this may be the cause of your problems.

The overridden set data method should set the value super.data =. You do not need to implement your own _data property, since Flex Containers implements IDataRenderer and already has one. I think that without running your code, you ran into a cache / recycle problem.

What I usually prefer to do is create a [Bindable] private myProperty: Object (usually it's a custom MyObjectVO that extends EventDispatcher). Then in my given data method, I will set super.data = value , and then if (value! = Null) myProperty = value .

This allows me to heavily enter my actual data, leaving the mechanisms intact with respect to the IDataRenderer interface implemented by Canvas (or another container), and ensures that the data is processed correctly.

Initialize the creation of announcements. Completed ones are bad evets in itemRenderers because they are actually redesigned and these methods do not behave as you might expect or want them.

I cannot express how useful this Peter Ent series on itemRenders was for me.

+1
source

Well, he is right, I have the same error, the first itemRenderer is called twice, you can see my message on the official forum here:

http://forums.adobe.com/thread/604259

and here the error is populated:

https://bugs.adobe.com/jira/browse/SDK-26010

0
source

All Articles