0

Is there any better way for passing data between component and its renderers (also calling methods)?
I am using dispatchEvent(new CustomEvent(CustomEvent.SOME_EVENT, data)); or owner.dispatchEvent(new CustomEvent(CustomEvent.SOME_EVENT, data)); or bubbling (depends on situation). And then listening in that component for my event.

And for passing data to renderer, I am using:
(myList.dataGroup.getElementAt(myList.selectedIndex) as MyRenderer).doSomething(data); or modifying dataProvider but then I have to callLater and call method inside renderer anyways.

This sucks, I have to callLater all the time for to wait for renderers set data function to execute, otherwise renderer is not recycled yet and is null.

Is there any more efficient, prettier way to transfer data and call functions between renderer and component?

P.S. And yes I have pass data to renderer. My renderer handles quite complicated logic.

randomUser56789
  • 936
  • 2
  • 13
  • 32
  • You can override data function of your renderer and call doSomething(data) inside set data function. This will automatically update your renderer. Ex: - override public function set data(value:Object):void { super.data = value; doSomething(value) } – Mahesh Parate Jul 30 '12 at 11:43
  • But if my renderer has many function (setting focus, selecting checkbox etc) and I don't want them to be called only when I update `dataProvider` (Purely visual stuff), I want them to be called on different occasions without touching `dataProvider`. As far as I know `set data` is only called when `dataProvider` gets changed. – randomUser56789 Jul 30 '12 at 11:49

2 Answers2

0

I've resolved a problem similar to yours in this way:

var goalPropDLRenderer:ClassFactory = 
         new ClassFactory(GoalPropagationDataListRenderer);

goalPropDLRenderer.properties = { isPropagationMode : isPropagationMode };

list.itemRenderer = goalPropDLRenderer;

Where: "list" is a list object for witch I've to set an item renderer; instead of setting the item renderer inline (on the "list" declaration via mxml), I've used this syntax. This code is called on the creationComplete of the "list" father (I think there are better places) In this way it's possible to pass "properties"/"values" to the renderer.

On GoalPropagationDataListRenderer I've:

[Bindable]
public var isPropagationMode : Boolean;

that gets populated by goalPropDLRenderer.properties = { isPropagationMode : isPropagationMode }; so inside the renderer I can access the value of isPropagationMode without problems.

I hope it could help.

Claudio
  • 11
  • 2
  • That solves data passing to renderer. But for example, I press button outside list and it has to set focus to list's selected index renderer's component, I think the only way is to `(myList.dataGroup.getElementAt(myList.selectedIndex) as MyRenderer).doSomething();` – randomUser56789 Jul 30 '12 at 14:30
  • When I have similar situations I use a property of the data object. If I have a list of "persons" and i want the item renderer to have some behavior I set a property of the person that the renderer could read via binding. So if the property is changed externally, by a button click the item renderer get updated. – Claudio Jul 30 '12 at 15:22
  • Yes, but I have a specific value object which I cannot modified by adding some properties. – randomUser56789 Jul 30 '12 at 21:40
  • You coult try to extend it or to add an attribute "online" without touching the original class. – Claudio Jul 31 '12 at 10:40
  • What do you mean by - attribute "online" ? – randomUser56789 Jul 31 '12 at 11:14
0

There are some ways, depends on what exactly you want to do (if the data are dependent to every row, or global in datagrid), but personally, I would avoid callLater and this you wrote: (myList.dataGroup.getElementAt(myList.selectedIndex) as MyRenderer).doSomething(data)

you can use bubbling event, then catch it in datagird (or whenever), and then get renderer through myRenderer(event.target), not needed to use getElementAt!, but questin is when would you dispatch the event (I dont know the details)

if you use global (in datagrid meaning) logic:

  • I would make some common object (called logic) which will be set in datagrid and renderers (separating logic from view is good practice anyway in my opinion)

  • then I would use the solution what Claudio wrote (setting properties to itemRenderer ClassFactory) to set the logic to renderer

  • or you can use listData object (but it's not as simple)
  • or if you write item renderer in mxml inside datagdid, you can easilly bind it using outerDocument like logic="{outerDocument.logic}"
  • or you can just use itemRenderer's owner property, so whenever yo want some methods, call like MyDataGrid(owner).doSomeDataGridCalculationsForData(data), not needed to create logic object if you dont want to
mflow
  • 11
  • 1