我就按自己的理解隨便解釋一下:首先要明白元數(shù)據(jù)不是語(yǔ)法的一部分,而是專(zhuān)門(mén)給編譯器用的,說(shuō)白了是告訴編譯器做某些事情,學(xué)過(guò)java之類(lèi)的應(yīng)該知道。那Bindable來(lái)講,它的作用是告訴 flex編譯器,給某些某些東西建立綁定關(guān)系,flex編譯器會(huì)在編譯過(guò)程中給AS(flex編譯器就是把mxml編譯成as,再編譯到swf,也可能直接編譯倒swf,我這里假設(shè)有as這么個(gè)環(huán)節(jié))加一點(diǎn)事件發(fā)生和處理之類(lèi)的代碼,由此綁定的關(guān)系便建立了,如果我們用純粹as3代碼來(lái)寫(xiě)也是可以實(shí)現(xiàn)的,就是太太太麻煩。 什么是綁定: 舉個(gè)例子:給下面的public變量加上[Bindable] [Bindable] public var name:String = ""; 作為一個(gè)public變量,肯定既可以被賦值,也能賦值給別的變量。綁定的作用就是,當(dāng)name改變的時(shí)候(被賦值了),可能通知其它被name影響(賦值給它們)的變量發(fā)生改變。這里的“可能”就需要編譯器來(lái)判斷,這就是為什么元數(shù)據(jù)是給編譯器用的原因了。在mxml里用{}的語(yǔ)法的地方就是綁定的對(duì)象,比如label={xxx.name},當(dāng)name變化,label也跟著變化。這樣,我們只是很簡(jiǎn)單的改變了name的值,由于有綁定,界面上的 label也跟著自動(dòng)變化了,爽吧。 能用在哪里 三個(gè)地方:類(lèi), 變量, getter/setter。是不是public沒(méi)有關(guān)系,private的就只能給自家用唄。用在Class上就是簡(jiǎn)單的給所有的public屬性(包括變量,getter/setter,普通方法)加上 [Bindable],可是一般的方法不能用[Bindable]呀,于是一般就能看到flex給了個(gè)warning,直接無(wú)視:)。變量嘛就是上面講的,很簡(jiǎn)單略掉。 用在只讀,只寫(xiě)屬性(getter/setter)上面 終于講到關(guān)鍵地方了,因?yàn)間etter和setter很像方法,用起來(lái)會(huì)有點(diǎn)不同??纯催@個(gè)例子:
復(fù)制代碼 代碼如下:
[Bindable] private var content:Array = new Array(); [Bindable] public function set _content(ct:String):void { content = ct.split(SEP); } [Bindable] public function get _wholeText():String { if(content.length == 0) { return ""; } else { var _w:String = ""; for(var i:int=0 ; i<content.length ; i++) { _w += content[i] + "/r/n"; } return _w; } }
原來(lái)的設(shè)想是content綁定_wholeText,可它是不工作的。為什么?_wholeText太復(fù)雜了,被編譯器排除在“可能”之外,編譯器認(rèn)為沒(méi)有綁定關(guān)系,如果只是簡(jiǎn)單的return content,倒是可以的。我這里搜到了一些比較權(quán)威的解釋。來(lái)自http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flex找到Ely Greenfield講的。 Now keep in mind that there's no way for the compiler to actually tell if the value of a property get function would be different if called, short of doing an extensive code flow analysis of the get function, identifying all the inputs that might be affecting the value of the get function (i.e., member fields, statics, globals that are used in the get function and in any methods, global functions, closures, etc) it might call, and setting up watchers on every one of those to trigger the binding when any of them change. That's prohibitively difficult, and expensive to do. So the compiler doesn't try. Instead when you put [Bindable] on a get/set property, the compiler makes it bindable with a little creative rewriting that allows the framework to watch the get function, and dispatch a change event when the get function is triggered. This means that automatic bindable properties don't work when the get function is computed from multiple values, or when you change its value by setting a backing field, rather than using the set function. It _also_ means that if you have no set function, we can pretty much guarantee that there's no way automatically bindable get properties will be triggered. a read only propeerty is, to the compiler, completely opaque…at the moment, it has no idea where that value is coming from, and hence will never be able to ‘a(chǎn)utomatically' trigger the binding. 說(shuō)白了就是為了降低復(fù)雜度和提高效率,復(fù)雜情況的getter會(huì)被忽略。如何解決?可以手動(dòng)建立綁定,即[Bindable("eventName")]。把代碼改成這樣:
復(fù)制代碼 代碼如下:
[Bindable] private var content:Array = new Array(); [Bindable] public function set _content(ct:String):void { content = ct.split(SEP); this.dispatchEvent(new Event("_contectChanged")); } [Bindable("_contectChanged")] public function get _wholeText():String { if(content.length == 0) { return ""; } else { var _w:String = ""; for(var i:int=0 ; i<content.length ; i++) { _w += content[i] + "/r/n"; } return _w; } }