How to access global state from ItemRenderer
Providing list based controls with a collection of data items is enough in most cases. But what if your renderer needs to be aware about some external state to decide on what style to apply to you data? There are several possible solutions here:
- to create a wrapper object for each data item and feed the list with collection of wrappers storing the original data items and some additional context. Good, but smells a little bit overkill.
- to get the renderer's owner and access its labelFunction or whatever is needed. This couples you to your parent list which might be not good if you want to reuse the renderer elsewhere.
- to have the static accessor to the global state. The same as above. Couples you to static.
People tend to forget that item renderers are generated by means of IFactory instances which allow them to configure renderers while constracting them.
Flex comes with ClassFactory implementation from the box. However passing parameters to it might be not a perfect idea because of it being not refactoring friendly when feeding your renderer's property by its field name.
As the alternative you might want to create your own implementation of IFactory and pass the context to the renderer explicitly.
Elegant enough and loosely coupled ;)