Posted by: ranvijay | December 9, 2010

Passing data to Flex ItemRenderers

Well, this took me some time to figure out. I was wondering for quite some time about a neat way to pass some data to my custom itemRenderer. Well, let me explain a bit what I was trying to achieve. I was trying to render a combobox in a datagrid column and the tricky part was that the dataprovider of the combobox was not static. So harcoding of the combobox dataprovider values inside the itemRenderer was not an option for me. Oh, I forgot to mention that since the datagrid was a dynamic one in my case, the datagrid was being created through actionscript and I was using ClassFactory to set the itemRenderer.

One workable option came out to be store the dynamic ‘combobox dataprovider’ in the Application object, and then retrieve it from the Application from the itemRenderer. But that was neat enough, for the simple reason that why would I set something in the Application for the mere purpose of a storing datagrid column dataprovider.

Finally the solution came out to be to simple and elegant. It’s all about the ClassFactory. I was already using it for setting my customRenderer for the datagrid column. What I didn’t know about this class was that I could also set properties of my customRenderer using this class. Here is some comment from ClassFactory class:

var productRenderer:ClassFactory = new ClassFactory(ProductRenderer);
productRenderer.properties = { showProductImage: true };
myList.itemRenderer = productRenderer;

Here showProductImage is a property of type boolean in ProductRenderer class. Needless to say, you can set function and event handlers as well this way :)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.