Flex and [Bindable] (with Basic)
posted on 20 Jul 2007 15:51 by greendog
หลังจากบางคนที่ได้ทำการติดต่อเข้ามากับผม ผมพบว่าส่วนใหญ่ที่เป็นผู้เริ่มต้นนั้น ยังมีการสับสนกับ Mark up tag [Bindable] อยู่ สงสัยใช่ไหมครับว่ามันคืออะไร มันมีหน้าที่ในการระบุว่า object นี้สามารถนำไปใช้ได้อย่างเดียวหรือเปล่า วันนี้เราจะมาไขปริศนากันครับ
[Bindable] metadata tag with Basic
[Bindable] metadata tag การบอก compiler ให้ทำการอับเดตเมื่อเกิดการเปลี่ยนแปลงขึ้นกับตัว object ที่ เป็น data property ที่ bind เข้ากับ dataProvider property ของ control โดยทั่วไป
[Bindable] and [Bindable(event=event)]
ส่วนที่แตกต่างระหว่าง การประกาศ event name ติดไปกับ tag [Bindable] นั้น คือการที่เราประกาศ [Bindable] tag ไว้โดดๆ จะเป็นการบอกให้ compiler นั้นเข้ามากำหนด event ชื่อ propertyChange ซึ่งจะถูก dispatch เมื่อเกิดการเปลี่ยนแปลงขึ้นกับ data object ตัวนี้ครับ
What will happen if dont mark this tag?
ถ้าเราไม่ได้ระบุ [Bindable] metadata tag ให้กับ object แล้ว control จะ read ข้อมูล เป็น read-only ครับ โดยที่การเปลี่ยนแปลงของ data object ที่ถูก bind เป็น dataProvider property จะไม่ถูกอัพเดตในส่วนอื่นๆ ที่อ้างถึงด้วยครับ
[Bindable] with Class
สำหรับ การ mark tag ไว้ที่ หัวของ class นั้น จะเป็นการบอกให้ compiler ทำการกำหนด tag [Bindable] ให้กับ public property ทั้งหมด โดยรวมถึง getter และ setter ทั้งหมดด้วยครับ ซึ่งจะมีประโยชน์ในการนำ instance ของ class นี่ไปใช้ในการ bind เข้ากับ control ซึ่ง control หรือ custom control จะมี logic ในการดึงข้อมูลออกจาก class ได้เองครับ
สำหรับการ mark tag class แบบนี้ compiler จะไม่มีการกำหนด property ที่เป็น protected หรือ private ทั้งสิ้น การที่จะให้ property เหล่านี้เป็น bindable ต้องไปกำหนดเองนะครับ
ทั้งหมดนี้คือ หลักการง่ายๆ กับ Bindable metadata tag ครับ ครั้งหน้าเรามาลองดูส่วน advanced สำหรับ [Bindable] metadata tag กันครับ
คนที่สนใจด้านนี้เหมือนกัน ยินดีให้คำแนะนำแลกเปลี่ยนความคิดเห็นได้ครับ email: phattarin_s@hotmail.com
สำหรับตนที่ส่งคำถามมาทาง e-mail ให้ระบุหัวเมลล์ว่า flex-ans นะครับเพราะส่งกันเข้ามาเยอะเหลือเกิน ถ้ากรณีไหนน่าสนใจผมจะขอนำมา post ไว้เผื่อคนอื่นจะได้มีความรู้ด้วยนะครับ
In English
Thank for all of your interest in my article. I have found most of mail told about 'mark-up' tag named [Bindable] that has been used in actionscript 3 class, or in any Script tag on MXML file. You may confusing how to use this metadata tag in efficient. I am proundly to explain for you.
[Bindable] metadata tag with Basic
[Bindable] metadata tag would describe Flex SDK compiler to automatic update the control's property that is binding with this marked object. Generally, most of Flex's control use these object to bind with thier dataprovider property
[Bindable] and [Bindable(event=event)]The difference of thease declaration is [Bindable(event=event class)] will dispatch pre-defined event whenever marked object has changed.
What will happen if dont mark this tag?
If you didn't declare [Bindable] metadata tag to object. The framework will read object's value one time only. Any modification will not be affect to binding control.
[Bindable] with Class
If you marks this tag on the top of actionscript 3 class declaration. The compiler will be notice to define any public attribute, included with every pubilc accessor method ( get and set function ). This technique is useful to define marked class as value object (design pattern) to be binded with any control in your application.
for any protected and private property ( included with accessor function ) would not be know by compiler. You should marked them manually if you want to use.
These are the basic principle about Bindable metadatatag. Next time I would like to describe you about advanced principle about these most using tag in Flex framework.
For anyone, who has interest in this Adobe's framework. I am honour to know you, exhange some idea, or for any contract (Ha Ha). You can mailed me at phattarin_s@hotmail.com
PS. For any question, please notice me by put 'Flex-ans' in front of your mail's topic.
edit @ 17 Dec 2007 17:02:57 by หมาเขียว
ดูงงๆ นิดๆ แต่เข้าใจแระครับ
ขอบคุณมากนะครับ
#1 By เด็กชายปอ (125.24.218.103) on 2007-08-03 23:21