Flex pattern: data renderer has to know about data

Halo framework provides a lot of out-of-shelf control classes that perform layout tasks, such as DataGrid and Tree. What they do is instantiating renderers based on their data provider and layout the renderers according to their own rules. In the Model/View architecture, these layout managers represent the view and the data provider object represents the model. Inside the trench, all the data renderers implement mx.core.IDataRenderer which requires them to have setter/getter for their data member.

The data setter method is the place where we hook up the renderer with its data; you normally also invalidate the component and/or fire events here when necessary. This also means unluckily we cannot stick in any kind of dataprovider and any kind of data renderers into the creator component and expect the latter to “automagically” hook up the data with the renderers. All the creator component do (and should be doing but not more) is to hook up data item with the individual renderer:

var renderer:UIComponent = _itemRendererFactory.newInstance();
IDataRenderer(renderer).data = _items[i];
renderers[i] = renderer;
addChild(renderer);

It’s individual renderer’s job to decide on how he wants to render the data and the hook is the data setter method. The following code shows how mx.controls.Button implements data interface.

private var _data:Object;

[Bindable(“dataChange”)]
[Inspectable(environment=”none”)]

/**
*  The <code>data</code> property lets you pass a value
*  to the component when you use it as an item renderer or item editor.
*  You typically use data binding to bind a field of the <code>data</code>
*  property to a property of this component.
*  ……
*/
public function get data():Object
{
return _data;
}

/**
*  @private
*/
public function set data(value:Object):void
{
_data = value;

//render the data we get
//dispatch the event
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}

So one data renderer would require certain format of dataprovider which may be different than that for another type of renderer. A real world analog is the manager only handles a piece of work to an individual and it’s up to that individual to decide how to process it, which is not the concern of the manager since the manager knows every little about the work itself; what the manager does is to choreograph the whole effort (setting-up, dividing, hooking up, coordinating, etc. of the data renderers) as a team. (Sort of 1 foot view vs. 5,000 feet view;) It’s kinda hard to get the same level of detail at different distances.)

The bottom line is, the renderer has to know about the data in order to render them. Yeah, common sense…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: