Flex: DataBinding in depth (Part Three)

本文介绍了Flex中的三种数据绑定方法:常规的数据绑定、MXML中的所有属性绑定及使用ObjectProxy进行属性监听。通过示例展示了如何在自定义VO中实现属性变化的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Normal Data Binding for VO

 

package vo
{
	[Bindable]
	public class Student
	{
		public var id:int;
		public var name:String;
		public var gender:String;
		public function Student(id:int, name:String, gender:String)
		{
			this.id = id;
			this.name = name;
			this.gender = gender;
		}
	}
}

 

<?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">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			import vo.Student;
			[Bindable]
			private var student:Student = new Student(1, "Davy", "Male");
		
			private function handler(event:MouseEvent):void
			{
				student.id ++;
			}
		]]>
	</fx:Script>
	<s:Button label="{student.id}" click="handler(event)"/>
</s:Application>

     Comment:

        1) Every time we click the button, both the button label and the student.id will change accordingly.

        2) If we leave out the [Bindable] annotation for student in Application domain, we will get a warning but it still works.

        3) If we leave out the [Bindable] annotation for id in VO and we add this on var student, it will not work cause the id is not bindable.

 

 

2. Binding all properties for MXML

<fx:Metadata>
       [Bindable]
</fx:Metadata>

 

    Comment:

         1) Why it doesn't work?

 

3. Binding Using ObjectProxy

        1) Why using ObjectProxy?

               We using ObjectProxy to monitor the properties changes in our custom VO.

               In the previous charpter, we are using the custom event dispatched to monitor a specific attribute in our custom VO. But what if we want to monitor all the property changes in VO?

        2) How to use ObjectProxy?

<?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" 
			   creationComplete="setWatcher(event);"
			   minWidth="955" minHeight="600">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.PropertyChangeEvent;
			import mx.utils.ObjectProxy;
			
			import vo.Student;
			
			private var student:Student = new Student(1, "Davy", "Male");
			private var objectProxy:ObjectProxy;
			
			private function setWatcher(event:Event):void
			{
				objectProxy = new ObjectProxy(student);
				objectProxy.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onChange);
				
				objectProxy.id = 2;
				objectProxy.name = "Clay";
				objectProxy.gender = "Female";
			}
			private function onChange(event:Event):void
			{
				idLabel.text = objectProxy.id.toString();
			}
		]]>
	</fx:Script>
	
	<s:Label id="idLabel"/>
</s:Application>

 

package vo
{
	public class Student
	{
		public var id:int;
		public var name:String;
		public var gender:String;
		public function Student(id:int, name:String, gender:String)
		{
			this.id = id;
			this.name = name;
			this.gender = gender;
		}
	}
}

     Comment:

        1) Pay attention while changing the attribute, we are not changing that of VO's but changing that of ObjectProxy's.

        2) It works the same as we add [Bindable] annotation to all attributes in Student/Vo.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值