Pregunta

I know an ItemRenderer is a ClassFactory and that you can use the newInstance method of ClassFactory to get an instance of the ItemRenderer. My question, however, is is it possible to use methods of the ItemRenderer without using ClassFactory.newInstance()?

In my case I can't use this newInstance method because it doesn't keep the state.

Is there any way I can do this? Thanks!

¿Fue útil?

Solución

An ItemRenderer is a component, like any other. The itemRenderer property of a list based class has a value of a ClassFactory. If you have a reference to an instance of the itemRenderer component, you can call methods on it.

You cannot call a method on any component if an instance if that component instance has not been created yet. So to call a method on an itemRenderer without using ClassFactory.newInstance() you must manually create your own instance using the new keyword.

Otros consejos

You might want to implement the ItemRenderer as smart as it is needed to recreate the state depending in the data being set. On the other hand, make sure that the data contains everything needed. You barely want to interact with the renderers in a different scope then the renderer itself.

If it should necessary, a DataGroup dispatches a RendererExistence event when a renderer is added.

private function newList():List {
  const list:List = new List();
  list.addEventListener(FlexEvent.INITIALIZE, list_initializeHandler);
  return list;
}

private function list_initializeHandler(event:FlexEvent):void {
  const listBase:ListBase = ListBase(event.target),
      dataGroup:DataGroup = listBase.dataGroup;

  dataGroup.addEventListener(RendererExistenceEvent.RENDERER_ADD, dataGroup_rendererAddHandler);
  dataGroup.addEventListener(RendererExistenceEvent.RENDERER_REMOVE, dataGroup_rendererRemoveHandler);
}

private function dataGroup_rendererAddHandler(event:RendererExistenceEvent):void {
  // renderer added
}

private function dataGroup_rendererRemoveHandler(event:RendererExistenceEvent):void {
  // renderer removed
}

This is the way to go if you need to reference single item renderer instances.

Do you mean static functions and variables?

If you define a function (or variable, or const) as static, it is accessible via the class name, so you could define

class MyClass {
    public static const className:String="MyClass.className (const)";
    public static function getClassName():String {
        return "MyClass.getClassName (function)";
    }
}

trace(MyClass.className); //prints "MyClass.className (const)"
trace(MyClass.getClassName()); //prints MyClass.getClassName (function)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top