Flex数据绑定陷阱:常见的误用和错误(十)
作者:medcl 类型:编译 来源:9RIA.com天地会
第十条
Using two-way data binding for unsupported properties
对不支持的属性使用双向数据绑定
Flex 4 supports two-way data binding.
You can create the binding by adding @ in front of one of the curly braces, while leaving the other field unbound.
For example, if you run the application below and type a value in one text field the value is mirrored in the other text field:
Flex4支持双向绑定。
你可以添加@标志在大括号前,来创建双向绑定,而把其他地方解除绑定。
例如,如果你运行下面的这个程序,在一个文本框中输入一个值,输入的值将会镜像到另一个文本框:
<?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"
minWidth="1024" minHeight="768">
<s:TextInput text="@{someTextInput.text}"/>
<s:TextInput id="someTextInput" y="30"/>
</s:Application>
Two-way data binding works in most cases;
however, it does not work with style or effect properties.
It also does not work with the arguments property for RemoteObject or the request property for HttpService, RemoteObject, or WebService.
双向绑定在大多数情况下能工作成功;
然而,它不能绑定样式和效果等属性。
它也不能绑定远程对象RemoteObject的参数属性或者是HttpService, RemoteObject, or WebService的请求属性。
Take a look at the example below:
看下面这个列子:
<?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.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function resultHandler(event:ResultEvent):void
{
// handle result
}
protected function faultHandler(event:FaultEvent):void
{
// handle fault
}
]]>
</fx:Script>
<fx
eclarations>
<s:HTTPService id="service" url="http://localhost/someservice.php"
result="resultHandler(event)"
fault="faultHandler(event)">
<mx:request xmlns="">
<username>@{someTextInput.text}</username>
</mx:request>
</s:HTTPService>
</fx
eclarations>
<s:TextInput text="@{someTextInput.text}"/>
<s:TextInput id="someTextInput" y="30"/>
<s:Button click="service.send()" />
</s:Application>
The reason that the two-way data binding will fail is that both the source and target properties must be bindable
as well as readable and writable to allow two-way data binding.
双向绑定不能成功的原因是被绑定的源属性和目标属性二者都必须是可以绑定的并且是可读写。
Data binding can be a tremendous advantage, but there are potential drawbacks.
I recommend taking the time to ensure you are using it correctly and only when needed,
since data binding misuse can increase overhead, lead to memory leaks, and degrade application performance.
数据绑定可以是一个巨大的优势,但也有潜在的缺点。
我建议抽出宝贵时间,以确保您使用的是正确的,并且只有在需要时使用,
因为数据绑定滥用可以增加开销,导致内存泄漏,并降低应用程序的性能。
To learn more about data binding, see the resources below:
学习更多关于数据绑定的知识,可以看以下这些资源:
* Flex in a Week: Understanding data binding
* Flex Quick Starts: Using data binding
* Using Flex 4: Data binding
* AdvancED Flex 4 Book
- 上一篇:
- Flex数据绑定陷阱:常见的误用和错误(
- 没有相关教材
- 最近更新
