views:

733

answers:

2

I would like to let the data provided to a DataGrid decide how best it should be rendered (that is, let the data carry with it an object which will do the rendering).

For example, by creating a "Renderable" interface, which has a 'renderer:IFactory' property, then used as below:

<mx:DataGrid x="0" y="0" width="100%" dataProvider="{myDataProvider}">
 <mx:columns>
 <mx:DataGridColumn headerText="Task" width="100"
  itemRenderer="{(data as Renderable).renderer}"/>
 </mx:columns>
</mx:DataGrid>

But to do this, Renderable has to extend IEventDispatcher, which seems like a little much...

I've also tried using:

itemRenderer="{(data as Renderable).getRenderer()}"

Which does nothing (in fact, the getRenderer method never gets called).

Is there a better way to do this? Am I doing something fundamentally wrong?

Thanks!

A: 

Make getRenderer a [Bindable] property

an0nym0usc0ward
Tried that – no love.It seems like at least one of the problems is that bindings "don't work" though a cast. For example, "{(data as Foo).bar}" in an ItemRenderer won't bind while {data.bar} will bind.
David Wolever
Bindings through casts *do* work. It's needed especially if you compile with -compiler.show-actionscript-warnings and -compiler.show-binding-warnings.Make sure you are binding to a property getter, not a function.
an0nym0usc0ward
+1  A: 

I could be wrong but I think the "data" property you're referencing in the code sample above is the "data" for the top-level Container in your view and not for that particular row of the DataGrid. A few other approaches come to mind:

  1. Implement a single item renderer class that examines the data being passed to it and utilizes the proper item renderer for the type of data supplied.
  2. Implement a function in the view of your DataGrid that examines your dataProvider and returns the proper item renderer class; call this inside the DataGridColumn.itemRenderer property using a binding expression.
  3. Implement an subclass of DataGridColumn that has the logic baked into it to set the correct itemRenderer.

I would actually recommend against mixing "renderer data" that is View-specific with data from your Model. Unless you wrap the core model data with an object that exposes it along with the renderer (what some people call a ViewModel).

cliff.meyers
Thanks for the response.The reason I want to put mix the data and the view this way is that there could be different type of data coming out of the provider and I think there will be the least overall coupling if each datum knows how to render its self.
David Wolever