Source: csdn blog http://blog.csdn.net/lichkui/archive/2009/10/20/4704736.aspx An article on [Bindable] usage reposted by a netizen: What is metadata (metadata): [Bindable] is probably the most frequently used metadata in flex. I just want to explain it by myself: First of all, we need to understand that metadata is not part of the syntax, but for the compiler. To put it bluntly, it is to tell the compiler to do some things and should be known after learning Java and so on. In terms of Bindable, it is used to tell the flex compiler to bind something, And the flex compiler will give the ( The flex compiler is to compile mxml into as, compile it to SwF, or directly compile the SWF. I suppose there is such a link as here) and add a bit of Code such as event occurrence and processing, the binding relationship is established. If we use Writing with pure as3 code is also possible, which 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. Change. 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. 1. getter/setter. It doesn't matter if it is public. Private can only be used for users. 2. Class. It simply adds [Bindable] to all the public attributes of the class (including variables, Getter/setter, and common methods), but the general method cannot use [Bindable, so we can see that flex has given Warning. The variable is described above, which is simple and omitted. 3. It is used in read-only mode and only writes the attribute (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:
- [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 + "/R/N ";
- }
- Return _ w;
- }
- }
Copy code 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. Content. I found some authoritative explanations here. From http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flexfind 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 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 Watch the get function, and dispatch a change event when the get function is triggered. This means that the automatic Bindable properties don't work when 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 'automically '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:
- [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;
- }
- }
Copy code 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. |