Filed under: actionScript, components, development, flex, tutorial | Tags: actionScript, components, development, flex, mxml, tips
Assume that you are creating a component in MXML such as some multi-state cell renderer or something that implements IDataRenderer.
All mx.core.Container subclasses have a convenient little event hook called dataChange. Anytime you set a value to the data property, the component dispatches an event of type FlexEvent.DATA_CHANGE. To hook into this you simple access it in MXML like so:
<mx:VBox dataChange="do something"/>
With this in mind I suggest that those wishing to do more complex visual changes to their component that {binding} cannot address do the following:
- create a flag to indicate a data change has occured:
private var bDataChange:Boolean = false;
- connect the dataChange hook to the flag:
<mx:VBox dataChange="bDataChange = true;"/>
- address the data change in the call to update the display list:
override protected function updateDisplayList (unscaledWidth:Number, unscaledHeight:Number):void { super.updateDisplayList(unscaledWidth, unscaledHeight); if (bDataChange) { bDataChange = false; //do your custom logic here } }
The nice bit about this is that you are doing the necessary logic to update the display list when it makes sense to do so (via the already established update framework in the Flex API). It also reduces the need to override the getter/setter for the data property.
4 Comments so far
Leave a comment
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is useful to know. I was using the override getters and setters at one point for this a few years ago. I don’t even want to think about what I went through with the custom getters and setters and change dispatch and so on.
Comment by diamondtearz Tuesday, April 28, 2009 @ 7:50 pm[...] Quick hint: Complex Component Development in MXML « explorers’ club (tags: flex components) [...]
Pingback by links for 2009-04-28 | diamondTearz Wednesday, April 29, 2009 @ 12:01 am[...] Quick hint: Complex Component Development in MXML « explorers’ club (tags: flex components) [...]
Pingback by diamondTearz » links for 2009-04-28 Thursday, May 21, 2009 @ 6:17 amThanks you for news
Comment by Neonchik Sunday, May 24, 2009 @ 8:51 am