Avg. Rating 3.6
Tags:



Problem

Using an itemRenderer from MXML is very easy: Set the itemRenderer property to the class name of the itemRenderer - that's it. But things start to get complicated, if you want to customize some properties of this itemRenderer.

Solution

The solution to this problem is to build a function that wraps a ClassFactory around the itemRenderer class and that injects the necessary properties.

Detailed explanation

Here's the static function that does the job:
public static function createRendererWithProperties(renderer:Class,
properties:Object ):IFactory {
  var factory:ClassFactory = new ClassFactory(renderer);
  factory.properties = properties;
  return factory;
}

And here's a simple example that adds a Tooltip to each item in a list:

<mx:List dataProvider="{['Foo', 'Bar']}" itemRenderer="{createRendererWithProperties(Label, {toolTip: 'Hello'})}"/>

This approach is especially useful if you have a custom itemRenderer that you want to use slightly modified in different places.

Report abuse

Related recipes