Flex 引入了元數據標籤的概念,大多數人都使用過的 [Bindable] 標籤便是其中的 meta tag 之一,它在代碼中的作用就是向編譯器提供如何編譯程序的信息。實際上,這些標籤並沒有被編譯到生成的 SWF 文件中,而只是告訴編譯器如何生成 SWF 文件。
adobe 官網 http://livedocs.adobe.com/flex/3/html/help.html?content=metadata_3.html詳細講解了 16種 meta的使用,簡單如下表所示。
Tag |
Description |
[ArrayElementType] |
Defines the allowed data type of each element of an Array. |
[Bindable] |
Identifies a property that you can use as the source of a data binding expression. |
[DefaultProperty] |
Defines the name of the default property of the component when you use the component in an MXML file. |
[Deprecated] |
Marks a class or class element as deprecated so that the compiler can recognize it and issue a warning when the element is used in an application. |
[Effect] |
Defines the MXML property name for the effect. |
[Embed] |
Imports JPEG, GIF, PNG, SVG, and SWF files at compile time. Also imports image assets from SWC files. |
[Event] |
Defines the MXML property for an event and the data type of the event object that a component emits. |
[Exclude] |
Omits the class element from the Flex Builder tag inspector. The syntax is as follows:[Exclude(name="label", kind="property")] |
[ExcludeClass] |
Omits the class from the Flex Builder tag inspector. This is equivalent to the @private tag in ASDoc when applied to a class. |
[IconFile] |
Identifies the filename for the icon that represents the component in the Insert bar of Adobe Flex Builder. |
[Inspectable] |
Defines an attribute exposed to component users in the attribute hints and Tag inspector of Flex Builder. Also limits allowable values of the property. |
[InstanceType] |
Specifies the allowed data type of a property of type IDeferredInstance. |
[NonCommittingChangeEvent] |
Identifies an event as an interim trigger. |
[RemoteClass] |
Maps the ActionScript object to a Java object. |
[Style] |
Defines the MXML property for a style property for the component. |
[Transient] |
Identifies a property that should be omitted from data that is sent to the server when an ActionScript object is mapped to a Java object using [RemoteClass]. |
本人認爲,這其中常用的 Bindable , Embed , RemoteClass 這 3 種(本人是從開發業務程序角度來考慮,effect 等 ui 效果就暫且忽略了)。
1、 Bindable 標籤
Bindable 標籤應該是使用率最高的標籤,其功效就是將某個變量或者屬性綁定到目標對象上,比如業務應用經常要操作 datagrid ,此時就需將變量綁定到 datagrid.dataprovider 上,通過變量操作來顯示 datagrid 的數據變動。
[ Bindable ]
private var transWayLists:ArrayCollection;
...
<mx:AdvancedDataGrid dataProvider="{ transWayLists }"/>
2、 RemoteClass 標籤
該標籤用以映射 flex object 到 java object ,因此在寫 flex 程序與 java 程序交互之時特別有用,比如 flex 登錄程序總需發送登錄信息到 java 登錄服務,然後 java 登錄服務通過驗證返回登錄用戶信息,此時就需 flex user 映射到 java user ,如下:
flex :
[ Bindable ]
[RemoteClass ( alias="com. marcus .User" )]
public class User
{
public var userId:String;
public var businessId:String;
public var userName:String;
public var password:String;
public var passTimeout:Date;
...
}
java:
package com.marcus;
import java.util.Date;
public class User {
private String userId;
private String businessId;
private String userName;
private String password;
private Date passTimeout;
...
}
3、 Embed 標籤
該標籤用以嵌入 jpg 等資源文件,並在編譯的時候生成到 swf 文件中,因此嵌入資源文件過大時,會影響 swf加載速度。
[Embed(source='home.png')] // 綁定圖片home.png 給home_icon 類
private var home_icon:Class;
4、 ArrayElementType 標籤
該標籤用以標識數組中元素的數據類型,該 meta tag 並未列入本人的常用 meta tag 之一,爲何還要強調的原因就是本人認爲 ArrayElementType 標籤就是一個雞肋標籤,狗屁不是,建議大家慎用。