自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入分析Flex[Bindable]及使用方法

開發(fā) 后端
Flex有很多值得學(xué)習(xí)的地方,本文向大家介紹一下Flex[Bindable]的概念及使用方法,F(xiàn)lex[Bindable]大概又是Flex用得最多的元數(shù)據(jù)了。

在學(xué)習(xí)Flex的過程中,你可能會遇到Flex[Bindable]方面的問題,這里和大家分享一下,希望本文的介紹能讓你有所收獲。

深入分析Flex[Bindable]及使用方法

◆Flex[Bindable]大概又是Flex用得最多的元數(shù)據(jù)了。剛開始用用確實(shí)好簡單,效率真是沒的說。不過這幾天用著卻碰到了些問題,我自己搜集了些資料,想著有必要在blog里總結(jié)一下吧。
啥是元數(shù)據(jù)(metadata)

今天不曉得為什么livedoc.adobe.com這么慢,沒辦法,拿不到權(quán)威的解釋了。我就按自己的理解隨便解釋一下:首先要明白元數(shù)據(jù)不是語法的一部分,而是專門給編譯器用的,說白了是告訴編譯器做某些事情,學(xué)過java之類的應(yīng)該知道。那Bindable來講,它的作用是告訴flex編譯器,給某些某些東西建立綁定關(guān)系,flex編譯器會在編譯過程中給AS(flex編譯器就是把mxml編譯成as,再編譯到swf,也可能直接編譯倒swf,我這里假設(shè)有as這么個環(huán)節(jié))加一點(diǎn)事件發(fā)生和處理之類的代碼,由此綁定的關(guān)系便建立了,如果我們用純粹as3代碼來寫也是可以實(shí)現(xiàn)的,就是太太太麻煩。

舉個例子:給下面的public變量加上Flex[Bindable]

 

  1. Flex[Bindable]  
  2. publicvarname:String=""

◆作為一個public變量,肯定既可以被賦值,也能賦值給別的變量。綁定的作用就是,當(dāng)name改變的時候(被賦值了),可能通知其它被name影響(賦值給它們)的變量發(fā)生改變。這里的“可能”就需要編譯器來判斷,這就是為什么元數(shù)據(jù)是給編譯器用的原因了。在mxml里用{}的語法的地方就是綁定的對象,比如label={xxx.name},當(dāng)name變化,label也跟著變化。這樣,我們只是很簡單的改變了name的值,由于有綁定,界面上的label也跟著自動變化了,爽吧。
能用在哪里

三個地方:類,變量,getter/setter。是不是public沒有關(guān)系,private的就只能給自家用唄。用在Class上就是簡單的給所有的public屬性(包括變量,getter/setter,普通方法)加上Flex[Bindable],可是一般的方法不能用Flex[Bindable]呀,于是一般就能看到flex給了個warning,直接無視:)。變量嘛就是上面講的,很簡單略掉。
用在只讀,只寫屬性(getter/setter)上面

終于講到關(guān)鍵地方了,因?yàn)間etter和setter很像方法,用起來會有點(diǎn)不同。看看這個例子: 

  1. Flex[Bindable]  
  2. privatevarcontent:Array=newArray();  
  3. Flex[Bindable]  
  4. publicfunctionset_content(ct:String):void  
  5. {  
  6. content=ct.split(SEP);  
  7. }  
  8. Flex[Bindable]  
  9. publicfunctionget_wholeText():String  
  10. {  
  11. if(content.length==0)  
  12. {  
  13. return"";  
  14. }  
  15. else  
  16. {  
  17. var_w:String="";  
  18. for(vari:int=0;i<content.length;i++)  
  19. {  
  20. _w+=content[i]+"\r\n";  
  21. }  
  22. return_w;  
  23. }  
  24. }  
  25.  

 ◆原來的設(shè)想是content綁定_wholeText,可它是不工作的。為什么?_wholeText太復(fù)雜了,被編譯器排除在“可能”之外,編譯器認(rèn)為沒有綁定關(guān)系,如果只是簡單的returncontent,倒是可以的。我這里搜到了一些比較權(quán)威的解釋。來自http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flex找到ElyGreenfield講的。

  1. Nowkeepinmindthatthere’snowayforthecompilertoactuallytellifthevalueofapropertygetfunctionwouldbedifferentifcalled,  
  2. shortofdoinganextensivecodeflowanalysisofthegetfunction,  
  3. identifyingalltheinputsthatmightbeaffectingthevalueofthegetfunction  
  4. (i.e.,memberfields,statics,globalsthatareusedinthegetfunctionandinanymethods,globalfunctions,closures,etc)itmightcall,
  5. andsettingupwatchersoneveryoneofthosetotriggerthebindingwhenanyofthemchange.  
  6. That’sprohibitivelydifficult,andexpensivetodo.Sothecompilerdoesn’ttry.  
  7.  
  8. InsteadwhenyouputFlex[Bindable]onaget/setproperty,  
  9.  
  10. thecompilermakesitbindablewithalittlecreativerewritingthatallowstheframeworktowatchthegetfunction,  
  11. anddispatchachangeeventwhenthegetfunctionistriggered.Thismeansthatautomaticbindablepropertiesdon’  
  12. tworkwhenthegetfunctioniscomputedfrommultiplevalues,  
  13. orwhenyouchangeitsvaluebysettingabackingfield,ratherthanusingthesetfunction.  
  14.  
  15. It_also_meansthatifyouhavenosetfunction,  
  16.  
  17. wecanprettymuchguaranteethatthere’snowayautomaticallybindablegetpropertieswillbetriggered.areadonlypropeertyis,  
  18. tothecompiler,completelyopaque…atthemoment,ithasnoideawherethatvalueiscomingfrom,  
  19. andhencewillneverbeableto‘automatically’triggerthebinding.  

說白了就是為了降低復(fù)雜度和提高效率,復(fù)雜情況的getter會被忽略。如何解決?可以手動建立綁定,即[Bindable("eventName")]。把代碼改成這樣:

  1. Flex[Bindable]  
  2. privatevarcontent:Array=newArray();  
  3. Flex[Bindable]  
  4. publicfunctionset_content(ct:String):void  
  5. {  
  6. content=ct.split(SEP);  
  7. this.dispatchEvent(newEvent("_contectChanged"));  
  8. }  
  9. [Bindable("_contectChanged")]  
  10. publicfunctionget_wholeText():String  
  11. {  
  12. if(content.length==0)  
  13. {  
  14. return"";  
  15. }  
  16. else  
  17. {  
  18. var_w:String="";  
  19. for(vari:int=0;i<content.length;i++)  
  20. {  
  21. _w+=content[i]+"\r\n";  
  22. }  
  23. return_w;  
  24. }  
  25. }  
  26.  

 這樣就避免了編譯器去自動識別。自己加上綁定關(guān)系,當(dāng)_content被賦值,發(fā)出_contentChanged事件,通知所有被綁定的getter方法執(zhí)行一遍。這也說明了,綁定不過是事件游戲而已,flex為用戶隱藏了很多底層算法。

【編輯推薦】

  1. Flex頁面跳轉(zhuǎn)實(shí)現(xiàn)的幾種方式
  2. FlexBuilder4十大新特性閃亮登場
  3. Flex框架中Cairngorm和Mate的優(yōu)點(diǎn)大比拼
  4. FlexBuilder3.0與Eclipse3.4的完美結(jié)合
  5. 解析Flex應(yīng)用開發(fā)步驟 新特性和技術(shù)框架 
責(zé)任編輯:佚名 來源: 163.com
相關(guān)推薦

2010-09-07 14:21:22

PPPoE協(xié)議

2022-04-12 08:30:45

TomcatWeb 應(yīng)用Servlet

2011-03-23 11:01:55

LAMP 架構(gòu)

2009-09-09 09:26:00

2010-03-08 14:53:48

Linux分區(qū)

2011-09-01 13:51:52

JavaScript

2023-02-01 08:13:30

Redis內(nèi)存碎片

2010-01-26 09:31:31

千兆接入交換機(jī)

2022-08-30 07:00:18

執(zhí)行引擎Hotspot虛擬機(jī)

2021-10-29 16:36:53

AMSAndroidActivityMan

2009-12-14 14:50:46

Ruby傳參數(shù)

2009-12-16 16:39:01

Visual Stud

2009-06-10 18:12:38

Equinox動態(tài)化OSGi動態(tài)化

2017-08-18 14:01:44

大數(shù)據(jù)dataWrangle

2009-12-22 15:39:36

IPPBX技術(shù)

2010-01-05 15:32:48

交換機(jī)技術(shù)

2009-12-08 18:02:06

PHP final關(guān)鍵

2018-12-18 10:11:37

軟件復(fù)雜度軟件系統(tǒng)軟件開發(fā)

2015-08-03 09:54:26

Java線程Java

2018-10-25 15:24:10

ThreadLocal內(nèi)存泄漏Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號