explorers’ club


Quick hint: Complex Component Development in MXML
Thursday, April 23, 2009, 6:36 pm
Filed under: actionScript, components, development, flex, tutorial | Tags: , , , , ,

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

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

[...] Quick hint: Complex Component Development in MXML « explorers’ club (tags: flex components) [...]

Pingback by links for 2009-04-28 | diamondTearz

[...] Quick hint: Complex Component Development in MXML « explorers’ club (tags: flex components) [...]

Pingback by diamondTearz » links for 2009-04-28

Thanks you for news

Comment by Neonchik




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>