Search code examples
actionscript-3apache-flexinvalidation

Which invalidate method to use


I am a bit confused about which invalidate method to use and when to use it. I need to change x and y of a component and in that case one should call a invalidation method for optimization, but i don't know which one and when exactly

target.addElement(node);
node.x = 100 + target.horizontalScrollPosition;
node.y = 100 + target.verticalScrollPosition;

node and target are both Groups


Solution

  • It depends on the component and perhaps you don't have to call any at all. From the given piece of code i'd say it's invalidateSize(). But containers usually make a good job of measuring their dimensions property. invalidateDisplayListmight be a good call, if you need to change the way the component is displayed.

    So, generally speaking, it depends on the component (super type etc) you are implementing.

    Edit: As both instances are groups, you shouldn't call any invalidation methods at all. You would only call the methods when implementing a custom component with additional properties. In the case of Groups, anything has been done for you in advance. The component live cycle is implemented and the various layouts provide a comfortable level of indirection.

    When you extend Group (or any other component) then you should be familiar with the component live cycle.

    Rule of thumbs:

    1. ignore the invalidation calls in pure MXML as it is done by the MXML compiler and the components themselves.
    2. use the invalidation calls in overridden setters, which mutate the state of the component (even in MXML). This usually leads to a clean yet simple design of components if the setters are used everywhere - even inside the components private methods.
    3. use validateSize, validateNow etc carefully, as these are simple synchronous shortcuts avoiding the component live cycle.

    The invalidation live cycles is based on the flash players elastic race track, which which divides rendering and processing of the data in different aspects processing the code.

    Further readings regarding the idea behind the invalidation calls: Updated elastic racetrack[1] and The Elastic racetrack[2]

    1. [1]: http://www.craftymind.com/updated-elastic-racetrack-for-flash-9-and-avm2/
    2. [2]: http://tedpatrick.com/2005/07/19/flash-player-mental-model-the-elastic-racetrack/