flex, component's life-cycle
这篇文章帮了我很大的忙,转了
Earlier today I fixed a minor code issue. It took seconds to identify, but I remember being a little stumped by a similar problem in my early Flex days. The difference now is that I'm familiar with the component life-cycle.
You may think it is only worth reading up on the Flex component life-cycle if you intend to create advanced components, but that's not true, even when you are doing nothing other than extending basic mxml components a little knowledge goes a long way.
To demonstrate the problem take a look at the init
method for this simple mxml component called TimeInputBar
(which extends Canvas
).
//<code>Init</code> is called when the component's <code>initialize</code> event is fired: public var time:Number; public function init():void { time = 0; }
That's simple enough. Now take a look at the ActionScript which is creating an instance of the TimeInputBar:
var t:TimeInputBar = new TimeInputBar(); t.time = 12; addChild(t);
Again, simple code, but did you spot the problem? With a little bit of knowledge it's easy to see that time
will always be reset to 0 and will never be 12. Why?
When you create a component using the new
operator, only part of the life-cycle completes. The component gets as far as the configuration stage, which means you can set properties on the component (to be processed by the component later) but the life-cycle will not go any further than that. It is in effect, paused.
However, all the really cool stuff in the life-cycle, like creating the component's child elements, measuring the component, drawing the component and dispatching events happens during the next stage.
It is only when you add the component to the display list using addChild
or addChildAt
that the life-cycle continues (in other words ... it continues when the component has a parent).
As mentioned, the next phase is the one in which the cool stuff happens: In more detail, this means that the preinitialize
event is dispatched, the component's children are created, the initialize
event is dispatched and the component's validation methods are called (those are beyond the scope of this article) and if that's not enough for you, when all that is done then everybody's favourite event, creationComplete
is dispatched.
So, with that knowledge in hand let's revisit the order of execution for the original example:
- Create component using 'new' operator
- Set
time
variable to 12 - Add component to display list
- initialize event is fired
init()
method is calledtime
variable is set to 0
Many beginners make the assumption that the preinitialize
and initialize
events are dispatched early on - don't beat yourself up, that's a reasonable assumption to make. However those events may actually be dispatched after you've set initial values for some of your component's properties.
So, what's the solution? We could simply swap the order of our code:
//no worky! t.time = 12; addChild(t); //worky addChild(t); t.time = 12;
...but I'm hoping you can see the issue; components should be just a little more flexible than that!
A perfectly acceptable solution here would be to perform a simple check inside our init method:
public var time:Number; private function init():void { if(isNaN(time)) time = 0; }
[Edit - it's also perfectly acceptable to set 'time' to 0 when you first declare it - this post is not really concerned with which solution you choose, but rather it uses simplified code to explain why such a problem may arise in the first place.]
Once that is done, it will not matter if you set time
before or after you add your component to the display list. If the value is set externally before init is called, the value will not be overridden.
You can read a little more about the Flex component life-cycle in one of my earlier posts here.
Feb 11, 2021 09:56:53 PM
Recently I'm working on a project and I need to use the component life-cycle. I search online and came across this article. https://thegouthelp.com Here the features and basic coding of the component life-cycle are shared which is very serviceable for me.
Sep 30, 2021 02:25:39 PM
you are the one who developed an interest in me towards coding. And you are helping a lot to find the right information about coding and programming. love to vote with you. best online engagement rings store Thank you for having me here.
Nov 07, 2021 11:23:39 AM
Thank you so much for sharing this with us. buildingblockcapital It was very helpful for me to know about this one from here. There are many things that I came to know about the life cycle from the post.
Dec 30, 2021 03:05:34 PM
I have gone through the data that is shared especially the program content that is added here which makes it easier for us to understand the minor code issue in a better way so I thank you for sharing this informative Post with us Tech Businessman From Maui
Apr 09, 2022 06:45:46 PM
thanks this is good blog. 온라인바카라
May 25, 2022 07:28:53 PM
I am not really excellent with English but I line up this really easygoing to understand . 먹튀검증
Jun 04, 2022 03:39:39 AM
Well worded post will be sharing this with my readers this evening tilta
Jun 13, 2022 12:23:25 AM
You’re the best, I was doing a google search and your site came up for foreclosures in Oviedo, FL but anyway, I have thoroughly enjoyed reading it, keep it up! ipad回收
Aug 14, 2022 04:52:13 AM
ให้บริการสล็อตออนไลน์ เล่น Slot777 ทางเข้า ฟรีเครดิต ผ่านมือถือ เว็บตรง ไม่ผ่านเอเย่นต์ ที่ปรับให้โอกาสเกมสล็อต แตกบ่อยกว่าที่อื่น ๆ. slot777
Aug 14, 2022 07:19:03 PM
Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. commercial window tinting sacramento
Aug 15, 2022 06:57:43 PM
I would like to say that this blog really convinced me to do it! Thanks, very good post. Mascarillas caseras para el acné
Aug 18, 2022 11:42:44 PM
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post! 오피사이트
Aug 19, 2022 03:29:40 PM
Great artical, I unfortunately had some problems printing this artcle out, The print formating looks a little screwed over, something you might want to look into. 아벤주소
Aug 20, 2022 11:52:39 PM
I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! Online booking
Aug 23, 2022 05:10:16 AM
Great post, thank you so much for sharing. Do you happen to have an RSS feed I can subscribe to? 온라인바카라
Aug 24, 2022 08:32:09 PM
Hello! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions? acne treatment
Aug 25, 2022 05:38:13 PM
Oh my goodness! a fantastic post dude. Thanks Nevertheless My business is experiencing issue with ur rss . Don’t know why Not able to sign up for it. Possibly there is any person obtaining identical rss difficulty? Anyone who knows kindly respond. Thnkx 온라인바카라
Aug 25, 2022 08:17:53 PM
Very efficiently written information. It will be advantageous to anyone who utilizes it, including me. Maintain the nice work. For certain i will take a look at more posts. This website seems to obtain a good amount of visitors. Order online
Oct 02, 2022 06:36:14 PM
Admiring the time and energy you put into your blog and in depth information you offer. It’s good to come across a blog every once in a while that isn’t the same old rehashed material. Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account. iPhone 14
Nov 01, 2022 09:50:57 PM
I’m curious to find out what blog platform you happen to be using? I’m having some small security problems with my latest website and I would like to find something more risk-free. Do you have any solutions? 카지노사이트
Nov 18, 2022 09:34:35 PM
my sister loves the tv series Ghost Whisperer but i do find it a bit distracting sometimes;; 온라인카지노
Jul 20, 2023 10:46:06 PM
Manchmal sind wir schockiert oder besorgt über einige unerwartete Apple-Rechnungsbelastungen, die in Form von Textnachrichten oder E-Mails durchscheinen. Diese Arten von ungewohnten Zahlungen werden häufig auf apple com bill kündigen für ihre Apple-ID-Kunden angezeigt, die sich nach monatlichen und jährlichen Kontoauszugstransaktionen erkundigen. Dies liegt daran, dass ihre Apple-IDs gehackt wurden oder Kreditkarteninformationen entwendet wurden und ein unschuldiges Kind oder Familienmitglied ohne ihr Wissen eingekauft hat.
Jan 12, 2024 01:37:43 AM
Pavzi.com is a startup by passionate webmasters and bloggers who have a passion for providing engaging content that is accurate, interesting, and worthy to read. pavzi.com We are more like a web community where you can find different information, resources, and topics on day-to-day incidents or news. We provide you with the finest web content on every topic possible with the help of the editorial and content team.