[Bindable] usage in flex

Source: Internet
Author: User
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:

  1. [Bindable]
  2. Private var content: array = new array ();
  3. [Bindable]
  4. Public Function SET _ content (CT: string): void
  5. {
  6. Content = CT. Split (SEP );
  7. }
  8. [Bindable]
  9. Public Function get _ wholetext (): String
  10. {
  11. If (content. Length = 0)
  12. {
  13. Return "";
  14. }
  15. Else
  16. {
  17. VaR _ w: String = "";
  18. For (var I: Int = 0; I <content. length; I ++)
  19. {
  20. _ W + = content + "/R/N ";
  21. }
  22. Return _ w;
  23. }
  24. }

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:

  1. [Bindable]
  2. Private var content: array = new array ();
  3. [Bindable]
  4. Public Function SET _ content (CT: string): void
  5. {
  6. Content = CT. Split (SEP );
  7. This. dispatchevent (new event ("_ contectchanged "));
  8. }
  9. [Bindable ("_ contectchanged")]
  10. Public Function get _ wholetext (): String
  11. {
  12. If (content. Length = 0)
  13. {
  14. Return "";
  15. }
  16. Else
  17. {
  18. VaR _ w: String = "";
  19. For (var I: Int = 0; I <content. length; I ++)
  20. {
  21. _ W + = content [I] + "/R/N ";
  22. }
  23. Return _ w;
  24. }
  25. }

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.

Related Article

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.