when should I override 1 of the lifecycle methods?
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?
First, I 100% recommend studying this presentation by EffectiveUI:
- Diving Deep with the Flex Component Lifecycle
and this by Michael Labriola from Digital Primates:
- Diving in the Data Binding Waters
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.
- Components are things that need to be nearly perfect, highly optimized, and extremely versatile/abstract.
- Views are things that you may only need in one Application, but could reuse if you so desired (LoginScreen, ContactForm, etc.).
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()
- Called once when component is constructed
- Called top down. So if
Panel
callscreateChildren
, it'screateChildren
method will calladdChild
on 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()
- If the parent doesn't have any sizing (percent or explicit), it will need to be sized based on it's children's sizes. This can only happen from the bottom up (took me quite a while to really wrap my head around that).
- If the parent has explicit or percent sizes, it skips this step.
You override measure
if you want to:
- Have
measuredWidth
ormeasuredHeight
return a useful value. So if you build a custom CoverFlowContainer component, andmeasuredWidth/measuredHeight
aren't set (becausemeasure
was not overriden), then if you don't specify any sizing on CoverFlowContainer, it would be 0 width 0 height. So instead, overridemeasure
and have it setmeasuredWidth
toradius * 2
or something like that, and now you don't need to give it a size! - If the component does not have an explicit or percent size, measure will be used to size the component. Otherwise it's skipped.
commitProperties
- Called after
measure
. - Applies all property changes (from setting properties on the component) to the component (they were stored in private variables for that first frame).
- Called a frame after initial property settings.
This is the most important method to override in my opinion. So for your CoverFlowContainer, say you set the hypothetical distance
, gap
, selectedItem
, and tilt
properties. When you set them, store them in private variables. Flex will wait a frame, and call commitProperties
. In your overridden commitProperties
, you can then say layout.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
- Called after
commitProperties
- Called top down.
You 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:
- TitleWindow (my own version)
- View3D (for Away3D/Papervision)
- Tree and Stack for Flex 4
- TextArea (with prompt, expandable, etc.)
- ToolTip (easier to skin tooltip)
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
, MenuView
and LoginView
, 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 own commitProperties
override.
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 showFontControls
and showStylePanel
. 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 overridden commitProperties
and updateDisplayList
to 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 a visible
property 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
Here's another presentation by Deepa (from the Flex framework team) that goes over a lot of the same framework methods, including a nice explanation of why the whole invalidation model exists to begin with:
http://tv.adobe.com/watch/max-2008-develop/creating-new-components-in-flex-3-by-deepa-subramaniam/
精彩评论