There are 2 main types of data that you need to pass to item renderers.
Rendered object data
This is usually passed through the data property of the renderer which of type Object, but you can cast it to the element type that the renderer displays. Depending on the type of renderer this property might be called listData, dataGridListData or treeListData.
General application data
There might be cases when an item renderer needs general application information that is not available in the rendered object data. For example, if you want to display a formated amount of money you might need to know the currency symbol which is generally stored in the application model.
WARNING!!! It is a very bad practice to hold a reference to the application model in the item renderer. The renderer should not be tied to anything else but the object that it needs to render.
So in order to push such a general value to the item renderer I identified two methods (there might be more).
1. Setting the properties object of the renderer ClassFactory. This is one of the cleanest way of passing the data into the renderer.
First you have to create a public property on the itemRenderer class which will hold the value of the data to pass in. For our example we will declare a public variable called currencySymbol of type String.
Then, when we assign the itemRenderer to the list component we have to create a ClassFactory of the item renderer class. On this ClassFactory object we can set a properties object which contains pairs of keys/values to be pushed in the renderer. In our case this object would be something like { currencySymbol: "$" }.
Then we simply assign the ClassFactory to the list as an itemRenderer.
//CustomRenderer.mxml
public var currencySymbol:String;
//List parent view class or mediator
var itemRenderer:ClassFactory = new ClassFactory(CustomRenderer);
itemRenderer.properties = {currencySymbol: model.currencySymbol};
list.itemRenderer = itemRenderer;
1. | //CustomRenderer.mxml |
2. | public var currencySymbol:String; |
3. | |
4. | //List parent view class or mediator |
5. | var itemRenderer:ClassFactory = new ClassFactory(CustomRenderer); |
6. | itemRenderer.properties = {currencySymbol: model.currencySymbol}; |
7. | |
8. | list.itemRenderer = itemRenderer; |
2. Setting the property through a static public variable. This can be done easily by creating a public static property called currencySymbol in our case and setting this before setting the class as an item renderer.
//CustomRenderer.mxml
public var currencySymbol:String;
//List parent view class or mediator
var itemRenderer:ClassFactory = new ClassFactory(CustomRenderer);
itemRenderer.properties = {currencySymbol: model.currencySymbol};
list.itemRenderer = itemRenderer;
1. | //CustomRenderer.mxml |
2. | public var currencySymbol:String; |
3. | |
4. | //List parent view class or mediator |
5. | var itemRenderer:ClassFactory = new ClassFactory(CustomRenderer); |
6. | itemRenderer.properties = {currencySymbol: model.currencySymbol}; |
7. | |
8. | list.itemRenderer = itemRenderer; |
These are 2 easy ways of pushing general information into the item renderer. As mentioned before you should NEVER hold references to external objects in the renderer.
Subscribe to:
Post Comments (Atom)
1 comments:
Your second piece of code is identical to the first one.
Could you correct it? thanks!!!
Post a Comment