Passing arguments to Item Renderers

When trying to create general-purpose item renderers in Flex, one of the first things I ran into was passing arguments. Passing arguments allows you to use the same item renderer in different ways.

The best solution I’ve been able to come up with is to create my own factory class:

package com.zacharypinter.util
{
   import flash.utils.Dictionary;
   
   import mx.core.IFactory;

   public class ArgumentsToRendererFactory implements IFactory
   {
      private var klass:Class;
      private var args:*;
      
      public function ArgumentsToRendererFactory(args:*,klass:Class) {
         this.args=args;
         this.klass=klass;
      }
      
      public function newInstance():*
      {
         var instance:Object = new klass;
         

         for (var key:String in args) {
            if (instance.hasOwnProperty(key)) {
               instance[key] = args[key];
            }
         }
         
         return instance;   
      }
      
   }
}

To use it, you can write something like this:

<mx:List 
   width="100%" height="100%" 
   dataProvider="{users}" 
   selectable="false" 
   itemRenderer="{new ArgumentsToRendererFactory({group:this.radioGroup},UserRenderer)}" 
/>

In the above example, I’m using the ArgumentsToRendererFactory to pass a radio group to every item renderer. This lets me use radio buttons inside a list component.

blog comments powered by Disqus