Usage of flex Bindable

Source: Internet
Author: User

 

What is metadata (metadata): [Bindable] is probably the most frequently used metadata in flex.
I just want to explain it as I can: First of all, we need to understand that metadata is not part of the syntax, but for the compiler. To put it bluntly, it tells the compiler to do something, I have learned Java and so on. In terms of Bindable, its function is to tell the flex compiler to establish binding relationships for some things. The flex compiler will compile mxml into as during the compilation process, if you compile it to SwF again, you may also directly compile the SWF. I suppose there is an as link here.) add a bit of Code such as event occurrence and processing, and then the binding relationship is established, if we use pure as3 code for writing, it is too troublesome.


What is binding:
For example, add [Bindable] to the public variable below.
[Bindable]
Public var name: String = "";
As a public variable, it can be either assigned a value or assigned to another variable. The binding function is to notify other variables affected by the name (assigned to them) when the name is changed. The "possibility" here requires the compiler to determine why metadata is used by the compiler. In mxml, the syntax of {} is the bound object, such as label = {XXX. name}. When the name changes, the label also changes. In this way, we simply changed the name value. Because of the binding, the label on the interface also changed automatically.

 

 

Where can I use it?
Three Places: Class, variable, Getter/setter. It doesn't matter if it is public. Private can only be used for users. In class, we simply add [Bindable] to all public attributes (including variables, Getter/setter, and common methods). However, [Bindable] cannot be used for general methods, so we can see that flex gave a warning and directly ignored it :). The variable is described above, which is simple and omitted.

Used in read-only and write-only attributes (getter/setter)
Finally, let's talk about the key point, because getter and setter are very similar and will be a little different in use. Let's take a look at this example:
Copy the Code as follows:
[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;
}
}

 

The original idea is to bind content to _ wholetext, but it does not work. Why? _ Wholetext is too complex and is excluded by the compiler from "possible". The Compiler considers that there is no binding relationship. If it is just a simple return content, it is acceptable. I found some authoritative explanations here. From http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flexto 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 wocould 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 'icically 'trigger the binding.

To put it bluntly, to reduce complexity and improve efficiency, the getter will be ignored in complicated cases. How can this problem be solved? You can manually create a binding, that is, [Bindable ("eventname")]. Change the code to the following:

Copy the Code as follows:
[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;
}
}

This avoids automatic identification by the compiler. Add the binding relationship on your own. When _ content is assigned a value, the _ contentchanged event is sent, notifying all bound getter methods to execute it again. This also shows that binding is just an event game. Flex hides many underlying algorithms for users.

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.