I’ve read in the flex developer guide that you sometimes need to override one
of the lifecycle methods like: commitProperties and updateDisplayList
but I’ve written a few flex apps without ever needing to implement them.
when do I need to override them?
I’ve read in the flex developer guide that you sometimes need to override one
Share
First, I 100% recommend studying this presentation by EffectiveUI:
and this by Michael Labriola from Digital Primates:
They go into things you’ll never find in the docs but that are super practical for understanding the Flex Component Lifecycle.
From my experience, the only time you need to worry about overriding core lifecycle methods is if you are creating components. I make a distinction between Application Views and the Components.
Views, for the most part, are just adding things to the display list of a more generic component (Canvas, Group, Container, VBox, List, etc.). You, as a View/Application developer, don’t really care about how the “dataProvider” creates it’s itemRenderers, it just works.
Components are a different story though. When you create a component, you want it to fit perfectly into that system Flex has set up: the Component Lifecycle. It’s pretty tough when you first try to build a component like they do, but after you wrap your head around it it’s super easy. Here’s how I think of the methods when I develop components:
createChildren()
PanelcallscreateChildren, it’screateChildrenmethod will calladdChildon all of it’s children, which callsinitialize, which callscreateChildren.If you created a custom component, say a StarRatingComponent, you might want to add 5 stars to the stage when the component is constructed. So you’d override
createChildren()to do add stars to the component you’re in. By default, though, all Container components in the Flex SDK add their children here (lists do it a bit differently), so you never have to do this if you’re building MXML views or something not-to-be-extremeley-reusable.The next 3 methods are called 1 frame after properties are set.
measure()
You override
measureif you want to:measuredWidthormeasuredHeightreturn a useful value. So if you build a custom CoverFlowContainer component, andmeasuredWidth/measuredHeightaren’t set (becausemeasurewas not overriden), then if you don’t specify any sizing on CoverFlowContainer, it would be 0 width 0 height. So instead, overridemeasureand have it setmeasuredWidthtoradius * 2or something like that, and now you don’t need to give it a size!commitProperties
measure.This is the most important method to override in my opinion. So for your CoverFlowContainer, say you set the hypothetical
distance,gap,selectedItem, andtiltproperties. When you set them, store them in private variables. Flex will wait a frame, and callcommitProperties. In your overriddencommitProperties, you can then saylayout.updateEverything(selectedItem, distance, gap, tilt);so to speak. So this is the method you override to make all property changes be applied at once.updateDisplayList
commitPropertiesYou only override this to set visible properties on the component, such as
setActualSize,graphics, etc. But by now (because of `commitProperties), you have all your required variables to update the display set to the right values.Overall
So from my experience, I worked a lot with these lifecycle methods when creating a component library for things I would use in a million projects:
I needed to make sure everything was updated and rendered perfectly according to the lifecycle. Reading and understanding the Flex 4 Spark Source Code really helps clarify when to override these methods. As does the Openflux Source Code (very simple, clear alternative to the Flex Framework. Not as feature rich so it shows how to bare-bone override those methods to accomplish some pretty advanced things).
When I develop applications and make things like
AdvertismentView,MenuViewandLoginView, I don’t ever think about it because all the components I’m using have already solved those problems (ViewStack, Group, List, etc.). I’m basically just setting properties they’ve defined and updated in their owncommitPropertiesoverride.The only time I would start overriding lifecycle methods in a View would be when I need to access custom variables from outside the view. So say I had a custom RichTextEditor and I created some properties called
showFontControlsandshowStylePanel. When I set those variables, I would probably do what they described in the Data Binding Presentation: accessor sets private variable and calls the invalidation methods, lifecycle methods execute a frame later and I have overriddencommitPropertiesandupdateDisplayListto show those panels and fonts. But in practice, that’s probably overkill because it wouldn’t offer that much of a performance gain for the amount of work it would take. I’d just set up some binding to avisibleproperty in that case. Nevertheless….The best thing to do to really get into this is to just download the Flex SDK Source and see what they’re doing.
Hope that helps.
Lance