Forgot to release and bear the risk of Internal Check Leakage
You can use
Tags or braces are used to implement the binding function. However, these methods produce overhead. In addition, you cannot use these technologies to remove bindings. If you want to optimize high-performance applications, you can useBindingUtils
Class to bind your object.BindingUtils
There are two ways to use the class:
bindProperty()
The static method is used to bind a common property.
bindSetter()
The method is a static method used to bind the setter function.
Let's take a look.bindProperty
Method signature:
public static function bindProperty( site:Object, prop:String, host:Object, chain:Object, commitOnly:Boolean = false, useWeakReference:Boolean = false):ChangeWatcher
Site and host parameters indicate the targetLocation
AndSource
Object. When the handler only calls a committed change event, you cancommitOnly
Settrue
When the handler can call a commit and non-commit change event, you can set the commitonlyfalse
(Default ).
useWeakReference
The parameter allows you to define that the reference of the host isStrongOrWeak. Strong reference (default) prevents the host from being garbage collected. However, weak references cannot do this. The following example contains a text input program and a simple component. When the textinput control is pre-initializedbindProperty
MethodpreinitializeHandler()
Function.
<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">
<FX: SCRIPT>
<! [CDATA [
ImportMX. Binding. utils. bindingutils;
ImportMX. Events. flexevent;
Protected FunctionPreinitializehandler (Event: flexevent ):Void
{
Bindingutils. bindproperty (label,"Text", Textinput,"Text");
}
]>
</FX: SCRIPT>
<S: layout>
<S: verticallayout/>
</S: layout>
<S: textinput id = "textinput" preinitialize = "preinitializehandler (event)"/>
<S: Label id = "label"/>
</S: Application>
Below isbindSetter
Example:
Public static function bindsetter (setter: function, host: object,
Chain: object,
Commitonly: Boolean = false,
Useweakreference: Boolean = false): changewatcher
setter
The parameter defines the setter method, so that when the current value of the chain is changed, the setter method can be called using the independent variable of this value. Wherehost
Indicates the source object, andchain
Attribute name.commitOnly
AnduseWeakReference
Parameters andbindProperty
Is the same.
Here is an example that usesbindSetter
:
<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">
<FX: SCRIPT>
<! [CDATA [
Import MX. Binding. utils. bindingutils;
Import MX. Events. flexevent;
Protected function preinitializehandler (Event: flexevent): void
{
Bindingutils. bindsetter (bindingsetter, textinput, "text ");
}
Private function bindingsetter (STR: string): void
{
Label. Text = STR;
}
]>
</FX: SCRIPT>
<S: layout>
<S: verticallayout/>
</S: layout>
<S: textinput id = "textinput" preinitialize = "preinitializehandler (event)"/>
<S: Label id = "label"/>
</S: Application>
In the background,ChangeWatcher
Class can beBindingUtils
Class. It supportsweak
Reference, so when you setuseWeakReference
Settrue
The garbage collector automatically cleans the host to avoid potential memory leakage.
When you useweak
When you use it to complete the task,ChangeWatcher
The object must be unmonitored. You are obligated to handle this task to ensure that there is no memory leakage. The best way to handle this task isbindProperty
The returned static method is assigned toChangeWatcher
Variable (because the static method can returnChangeWatcher
Instance, you can assign it to a variable ). You can useunwatch
Method, as you can see in the following example (see figure 5 ).
<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">
<FX: SCRIPT>
<! [CDATA [
Import MX. Binding. utils. changewatcher;
Import MX. Binding. utils. bindingutils;
Import MX. Events. flexevent;
Private var change: changewatcher;
Protected function preinitializehandler (Event: flexevent): void
{
Change = bindingutils. bindproperty (label, "text ",
Textinput, "text", false, true );
}
Protected function clickhandler (Event: mouseevent): void
{
Change. unwatch ();
Change = NULL;
}
]>
</FX: SCRIPT>
<S: layout>
<S: verticallayout/>
</S: layout>
<S: textinput id = "textinput"
Preinitialize = "preinitializehandler (event)"/>
<S: Label id = "label"/>
<S: button label = "Stop binding" Click = "clickhandler (event)"/>
</S: Application>
Figure 5.
One useBindingUtils
Class and use buttons to bind and remove simple applications
TextInput
Oftext
The property isLabel
Oftext
Property bound when youTextInput
The text data is copiedLabel
Componenttext
Attribute. When you are about to remove the binding, clickStop Binding
. This operation unmonitors the corresponding attributes and sets the corresponding objectnull
In this way, the object will be cleared during garbage collection.