flex 数据绑定之理解

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]-->

之前写了《 flex 事件之理解》和《 flex meta tag 之理解》,本想是为写本文 flex 数据绑定之理解做铺垫的,谁知网上已有 flex 数据绑定之理解很好的文章,如下:

http://www.cnblogs.com/nianshi/archive/2010/05/19/1739407.html FLEX 数据绑定 (1)

http://www.cnblogs.com/nianshi/archive/2010/05/19/1739409.html FLEX 数据绑定 (2)

http://www.cnblogs.com/nianshi/archive/2010/05/19/1739411.html FLEX 数据绑定 (3)

因此,已无再详片大论 flex 绑定理解之需要,但是鉴于上述 3 篇文章稍显混论,看起来不是那么通俗易懂,为此,我简单的补充了一个 demo ,并且作简单归纳总结。

<!-- [if !supportLists]-->1、 <!-- [endif]-->flex 绑定通常有 3 中实现方式,即 FLEX 数据绑定 (1) 所述的 {} 绑定、 fxbindingas 代码实现 ( BindingUtils , ChangeWatcher );

<!-- [if !supportLists]-->2、 <!-- [endif]-->flex 绑定分单、双向绑定,默认情况下都是单向绑定;

<!-- [if !supportLists]-->3、 <!-- [endif]-->flex 绑定最常见的编写方式就是 [Bingable] 元标签及 {} 的引用;

<!-- [if !supportLists]-->4、 <!-- [endif]-->flex 绑定基于事件,只不过 [Bingable] 等同于 [Bindable(event="propertyChange")] 而已;

<!-- [if !supportLists]-->5、 <!-- [endif]-->flex 绑定可以基于简单的变量、也可基于复杂的对象、方法和事件;

/

下述为 flex 3 种绑定方式实现的一个简单 demo

<?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=" 955 " minHeight=" 600 "

creationComplete="initWatcher()" >

<s:layout>

<s:BasicLayout/>

</s:layout>

<fx:Declarations>

<!-- 将非可视元素(例如服务、值对象)放在此处 -->

</fx:Declarations>

<fx:Script>

<![CDATA[

import mx.binding.utils.BindingUtils;

import mx.binding.utils.ChangeWatcher ;

import mx.events.FlexEvent;

public function updateMyString(val:String): void {

p3_target.text = val.toUpperCase();

}

public function mySetterBinding(event:FlexEvent): void {

var watcherSetter:ChangeWatcher =

BindingUtils.bindSetter(updateMyString, p3_source, "text" );

}

public function initWatcher(): void {

ChangeWatcher .watch(p4_source, "text" , watcherListener);

}

public function watcherListener(event:Event): void {

p4_target.text=p4_source.text.toUpperCase();

}

]]>

</fx:Script>

<fx:Binding source=" p2_source.text " destination=" p2_target.text " />

<s:Panel x=" 9 " y=" 6 " width=" 462 " height=" 70 " title=" 大括号绑定 " id=" panel1 " >

<s:Label x=" 6 " y=" 10 " text=" source: " />

<mx:TextInput id=" p1_source " text=" Enter text here " x=" 50 " y=" 4 " />

<s:Label x=" 259 " y=" 10 " text=" target: " />

<mx:Text id=" p1_target " text=" { p1_source.text.toUpperCase() } " x=" 307 " y=" 4 " />

</s:Panel>

<s:Panel x=" 9 " y=" 84 " width=" 462 " height=" 98 " title=" mx:binding " id=" panel2 " >

<s:Label x=" 6 " y=" 10 " text=" source: " />

<mx:TextInput id=" p2_source " text=" Enter text here " x=" 50 " y=" 4 " />

<s:Label x=" 259 " y=" 10 " text=" target: " />

<mx:Text id=" p2_target " text="" x=" 307 " y=" 4 " />

<s:Label x=" 7 " y=" 43 " text=" fx:binding 等同于 BindingUtils.bindProperty(p2_target,'text',p2_source,'text'); " />

</s:Panel>

<s:Panel x=" 9 " y=" 190 " width=" 462 " height=" 70 " title=" actionscript bindingbindSetter " id=" panel3 " >

<s:Label x=" 6 " y=" 10 " text=" source: " />

<mx:TextInput id=" p3_source " text=" Enter text here " x=" 50 " y=" 4 " />

<s:Label x=" 259 " y=" 10 " text=" target: " />

<mx:Text id=" p3_target " text="" x=" 307 " y=" 4 " initialize="mySetterBinding(event);" />

</s:Panel>

<s:Panel x=" 9 " y=" 272 " width=" 462 " height=" 70 " title=" actionscript bindingchangewatcher " id=" panel0 " >

<s:Label x=" 6 " y=" 10 " text=" source: " />

<mx:TextInput id=" p4_source " text=" Enter text here " x=" 50 " y=" 4 " />

<s:Label x=" 259 " y=" 10 " text=" target: " />

<mx:Text id=" p4_target " text="" x=" 307 " y=" 4 " />

</s:Panel>

</s:Application>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值