Flex 通用调试工具(ff,chrome,ie8+)
Integrating Flex into Ajax applications

flex, component's life-cycle

marz posted @ Aug 20, 2010 09:43:41 AM in flex with tags flex life-cycle tricks , 2400 readers

这篇文章帮了我很大的忙,转了

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 called
  • time 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.

ref:nwebb.co.uk/blog/

 

 

Harold said:
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.

charllyyy said:
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.

zaiya said:
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.

Dwyane said:
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

AAA said:
Apr 09, 2022 06:45:46 PM

thanks this is good blog. 온라인바카라

AAA said:
May 25, 2022 07:28:53 PM

I am not really excellent with English but I line up this really easygoing to understand . 먹튀검증

AAA said:
Jun 04, 2022 03:39:39 AM

Well worded post will be sharing this with my readers this evening tilta

AAA said:
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回收

meidir said:
Aug 14, 2022 04:52:13 AM

ให้บริการสล็อตออนไลน์ เล่น Slot777 ทางเข้า ฟรีเครดิต ผ่านมือถือ เว็บตรง ไม่ผ่านเอเย่นต์ ที่ปรับให้โอกาสเกมสล็อต แตกบ่อยกว่าที่อื่น ๆ. slot777

 
=====================
 
SLOT888 เป็นผู้ให้บริการ สล็อตออนไลน์ ที่ดีที่สุดในตอนนี้ สถานะการเงินมั่นคง ได้เงินจริง สล็อต888 เป็นศูนย์รวมของ เกมสล็อตออนไลน์ เกมยิงปลา แทงหวย บาคาร่า แทงบอล เว็บเดียวที่ครบวงจรที่สุด ที่ให้บริการ เกมสล็อตออนไลน์ เล่นง่ายไม่ต้องโหลด แอปพลีเคชัน สามารถเล่นผ่านมือถือได้เลย เล่นได้ทุกที่ทุกเวลา. slot888
 
======================
 
pg slot auto wallet ฝากถอน อัตโนมัติ - Easy Slot เว็บสล็อตออนไลน์ สะดวก มั่นคง ปลอดภัย สมัครได้เลย เล่นได้ตลอด 24 ชั่วโมง. pgslot auto ฝากถอน อัตโนมัติ
 
======================
 
สแกนสล็อต ถือเป็นตัวช่วยเล่นที่เหมาะสำหรับนักเดิมพันที่เล่นกับ slot ในปัจจุบัน สามารถสแกน ได้ครบทุกเกม โดยเฉพาะเกมที่มีอัตราจ่ายรางวัลโบนัสสูง. สแกนสล็อต
 
======================
 
Slot auto wallet สล็อต เติม ทรู วอลเลท สล็อต เติม true วอลเลท ฝาก-ถอน ไม่มี ขั้น ต่ํา 2022 สล็อต เติม truewallet ฝาก-ถอน ไม่มี ขั้น ต่ํา 2022 slot auto wallet สล็อตออโต้โปร 100 ฝาก 10 รับ 100 ล่าสุด วอเลท สล็อต ระบบออโต้ ออโต้. slot auto wallet
 
======================
 
จากการที่ในปัจจุบันนี้เรื่องของการเล่นเกมสล็อตออนไลน์กำลังเป็นที่นิยมกันเป็นอย่างมาก ไม่ว่าจะเป็นที่ไหนก็ตามจะมีการเล่นเกมสล็อตออนไลน์ ผ่านทางระบบมือถือของคุณกันตลอดทั้งวัน ซึ่งในเรื่องของค่าความนิยมที่ทำให้เกมสล็อตออนไลน์กำลังเป็นที่นิยมกันนั้น. เล่นสล็อต เว็บไหนดี pantip
 
======================
 
all slot auto wallet มาที่อีกหนึ่งรูปแบบเกมคาสิโนออนไลน์ ที่ตอนนี้กำลังมาแรงอย่างมากกับ สล็อต ทุกค่าย ซึ่งเกมสล็อตออนไลน์รูปแบบนี้ ผู้เล่นสามารถเข้ามาสมัครเล่นได้ทั้งในมือถือ และ เล่นบนเว็บในคอมพิวเตอร์ ของตัวเองได้เลย. all slot auto วอเลท
 
======================
 
เราคือ ผู้ให้บริการ สล็อตออนไลน์ JOKER AUTO WALLET เว็บตรง ผ่านการเติมเงินช่องทาง True Money Wallet บริการเติมเงินเกมสล็อต. joker auto wallet
 
======================
 
สล็อตเครดิตฟรี 50 ไม่ต้องแชร์ คืออะไรทำไมหลายต่อหลายคน จึงสนใจพอได้ยินคำว่า เครดิตดังกล่าว ต่างตาโต และ อยากที่จะรู้ว่า. สล็อตเครดิตฟรี 50
 
======================
 
ตารางเวลาเล่นสล็อต นั้นมีความสำคัญอย่างไรต่อการทำกำไรผ่านเกมสล็อต อย่างที่ได้กล่าวไปข้างต้นจำนวนผู้อยู่ในเกมสล็อต และ การออกรางวัลของสล็อตมีผลต่อการที่แจ็คพอตจะแตกอย่างมาก. ตารางเวลาเล่นสล็อต
 
======================
 
เครดิตฟรีไม่ต้องฝากไม่ต้องแชร์แค่สมัคร หากคุณกำลังมองหาเกมสล็อตที่สามารถเชื่อถือได้ ก็ต้องเว็บนี้เลย แจกรางวัลรัวๆ. https://xoslotz.com/เครดิตฟรีไม่ต้องฝากไม่/
 
======================
 
pg เครดิตฟรี กดรับ เอง เล่นผ่านมือถือ สมัครวันนี้รับฟรีเครดิต กดรับเอง เว็บใหญ่ที่สุด เกมเยอะที่สุด เล่นได้ทั้งวัน แตกง่าย แตกบ่อย การันตี. https://xoslotz.com/pg-เครดิตฟรี-กดรับ-เอง/
 
======================
 
slotxo wallet เว็บที่ดีที่สุดสำหรับนักเดิมพันปี 2022 เล่นง่าย ไม่ซับซ้อน แถมยังมีเงินรางวัลใหญ่ๆให้ลุ้นรับตลอดเวลาอีกด้วย. https://xoslotz.com/slotxo-wallet/
 
======================
 
เครดิตฟรี 50 ล่าสุด เกมสล็อตอันดับหนึ่ง แจกรางวัลสองเท่า มีให้เล่นมากมายหลายรูปแบบ เล่นได้ทั้งวัน ง่ายๆเพียงแค่ปลายนิ้ว. https://xoslotz.com/เครดิตฟรี-50-ล่าสุด/
 
======================
 
sagame 1688 สมัครเล่นสล็อตออนไลน์กับเว็บตรงไม่ผ่านเอเย่นต์ ฟรีเครดิต ฝากถอนไม่มีขั้นต่ำ เกมใหม่เล่นง่าย ทำกำไรได้งาม. https://xoslotz.com/sagame-1688-2/
 
======================
 
sagame 6699 สมัครเล่นสล็อตออนไลน์ รับฟรีเครดิต เกมสนุกมากมาย เล่นได้ทั้งวันไม่มีเบื่อ ฝากถอนไวไม่มีขั้นต่ำ. https://xoslotz.com/sagame-6699-2/
meidir said:
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

meidir said:
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é

meidir said:
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! 오피사이트

meidir said:
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. 아벤주소

meidir said:
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

meidir said:
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? 온라인바카라

meidir said:
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

meidir said:
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 온라인바카라

meidir said:
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

meidir said:
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

meidir said:
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? 카지노사이트

meidir said:
Nov 18, 2022 09:34:35 PM

my sister loves the tv series Ghost Whisperer but i do find it a bit distracting sometimes;; 온라인카지노

apple com bill kündi said:
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.

pavzi.com said:
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.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter