views:

40

answers:

1

My setup is pretty basic. I have an s:List with a custom itemRenderer and a dataprovider. What I would like to do is access the generated instances of the item renderer but I have no idea how.

Here is the code for the list:

<s:List id="layersList" 
            borderVisible="false"  
            allowMultipleSelection="true" 
            contentBackgroundAlpha="0" 
            itemRenderer="renderers.LayerRenderer" 
            dataProvider="{AssetsCollection}">
     <s:layout>
    <s:VerticalLayout gap="1"  />           
     </s:layout>
<s:list>

What I would like is to access the generated renderers like:

layersList.renderers[selectedIndex] or layersList.selectedItems[0].renderer. In order to access some of its internal objects. Like in the event I would want to listen events dispatched in the renderer instance from the List's parent.

Can anyone help?

+1  A: 

Hi Tarek, the conceptual model of Lists/ItemRenderers is that they are a representation of the items in the dataProvider. One reason for keeping this in mind is that Lists recycle their ItemRenderers in order to reduce memory footprint. This means you may have 100 items in your dataProvider, but only a small subset of those will have ItemRenderers associated with them, and some of those may not even be visible on screen or even valid any longer. There are a few ways you could approach having your ItemRenderers in your List reflect the state of the List's parent without having to directly manipulate the renderers. For instance, you could do something like this:

<s:List id="layersList" 
        borderVisible="false" 
        allowMultipleSelection="true" 
        dataProvider="{AssetsCollection}"
        contentBackgroundAlpha="0">
    <s:layout>
        <s:VerticalLayout gap="1" />      
    </s:layout>
    <s:itemRenderer>
        <fx:Component>
            <myrenderers:TestRenderer myState="{outerDocument.someState}"/>
        </fx:Component>
    </s:itemRenderer>
</s:List>

Where TestRenderer has a bindable public property called myState. And the List's parent has a bindable property called "someState". Then inside your renderer you can set some conditional logic based on the value of myState. Hope that helps.

Wade Mueller
Not quite the answer I was hoping for. But least, I understand why so thanks Wade.
Tarek
Just have a small follow up question. Aren't the renderers recycled only if virtual layout is enabled?
Tarek
Good point, useVirtualLayout is set to true by default. You could turn that off, but working directly with the renderers is not recommended and my experience with doing that has been problematic at best.
Wade Mueller
I agree, since setting it back to false would break my code. Just by curiosity, does setting it to false give access to the renderers?
Tarek