Data Binding in Flex 1: Principles of Data Binding

Source: Internet
Author: User

The [Bindable] label is provided in Flex to facilitate data binding. But what is the principle behind it? You can use the flash. utils. describeType tool for analysis.
Assume that the following classes declare Data Binding to member variables:

Package test
...{
Import mx. collections. ArrayCollection;

Public class BindablePropertity
...{
[Bindable]
Public var list: ArrayCollection = new ArrayCollection ();
}
}

The xml output by flash. utils. describeType is as follows:

<Type name = "test: BindablePropertity" base = "Class" isDynamic = "true" isFinal = "true" isStatic = "true">
<ExtendsClass type = "Class"/>
<ExtendsClass type = "Object"/>
<Accessor name = "prototype" access = "readonly" type = "*" declaredBy = "Class"/>
<Factory type = "test: BindablePropertity">
<ExtendsClass type = "Object"/>
<ImplementsInterface type = "flash. events: IEventDispatcher"/>
<Method name = "hasEventListener" declaredBy = "test: BindablePropertity" returnType = "Boolean">
<Parameter index = "1" type = "String" optional = "false"/>
</Method>
<Method name = "removeEventListener" declaredBy = "test: BindablePropertity" returnType = "void">
<Parameter index = "1" type = "String" optional = "false"/>
<Parameter index = "2" type = "Function" optional = "false"/>
<Parameter index = "3" type = "Boolean" optional = "true"/>
</Method>
<Method name = "willTrigger" declaredBy = "test: BindablePropertity" returnType = "Boolean">
<Parameter index = "1" type = "String" optional = "false"/>
</Method>
<Accessor name = "list" access = "readwrite" type = "mx. collections: ArrayCollection" declaredBy = "test: BindablePropertity">
<Metadata name = "Bindable">
<Arg key = "event" value = "propertyChange"/>
</Metadata>
</Accessor>
<Method name = "addEventListener" declaredBy = "test: BindablePropertity" returnType = "void">
<Parameter index = "1" type = "String" optional = "false"/>
<Parameter index = "2" type = "Function" optional = "false"/>
<Parameter index = "3" type = "Boolean" optional = "true"/>
<Parameter index = "4" type = "int" optional = "true"/>
<Parameter index = "5" type = "Boolean" optional = "true"/>
</Method>
<Method name = "dispatchEvent" declaredBy = "test: BindablePropertity" returnType = "Boolean">
<Parameter index = "1" type = "flash. events: Event" optional = "false"/>
</Method>
</Factory>
</Type>

It can be seen that after the [Bindable] Declaration is added, this class implements the IEventDispatcher interface, and the propertyChange event will be distributed when data changes. In this way, other components that listen to this event can be notified when data changes.
Most attributes of a Flex component can be bound with braces "{}", or the attributes of one component can be bound to the attributes of another component. Also, use describeType for analysis. You can see that:

<Accessor name = "btn1" access = "readwrite" type = "mx. controls: Button" declaredBy = "FlexFramework">
<Metadata name = "Bindable">
<Arg key = "event" value = "propertyChange"/>
</Metadata>
</Accessor>

The component updates data by listening to the propertyChange event. However, the preceding data binding statement does not specify the event name, because [Bindable] is a simplified method, which is equivalent to [Bindable (event = "propertyChange")]. The event name can be changed, but the Flex component listens to the event named propertyChange by default. If you change the event name, you must listen to the corresponding event in Flex and write the event processing.
The next article will introduce several examples of actual binding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.