`
luhantu
  • 浏览: 202939 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Flex 绑定机制 (ChangeWatcher & BindingUtils )

    博客分类:
  • Flex
阅读更多

Flex的绑定机制也是一个亮点,对于数据和显示分离提供很好的支持。在Flex绑定分为xml 绑定和纯As绑定。

这里不讲原理只讲用法,欲了解原理可以查看adobe官方文档,非常详细。

BindingUtils 其实是对ChangeWatcher的一个封装类,可以参考BindingUtils的两个方法的源码。

<?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" initialize="initializeHandler(event)">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import com.mode.clone.Company;
			import com.mode.clone.Person;
			
			import mx.binding.utils.BindingUtils;
			import mx.core.UIComponent;
			import mx.events.FlexEvent;
			[Bindable]/* 对于一个基本类型的属性来说,加上 [Bindable]标签默认是在属性改变时候触发绑定*/
			private var inputStr:String;
			/* 对于 复制类型的属性来说,如果绑定是person整个对象,那么只有在对person赋值的时候才会触发绑定.
			如果你想绑定person对象内部的一个属性,那么它内部的那个属性也必须加上[Bindable]标签。*/
			[Bindable]
			public var person:Person;
			[Bindable]
			public var asBindInputStr:String;/* 对于用as绑定的属性必须为public */
			protected function initializeHandler(event:FlexEvent):void
			{
				person ||= new Person();
				person.name = "kenny";
				var company:Company = new Company();
				company.name = "lomboard";
				person.company = company;
			}
			
			protected function changInputStrClickHandler(event:MouseEvent):void
			{
				asBindInputStr = inputStr = 'base one' + Math.random().toString();
			}
			
			[Bindable(event="customEvent")]
			private function bindFun():String
			{
				return "bindFun" + Math.random().toString();
			}
			
			[Bindable(event="customEvent")]
			public function asBindFun(host:UIComponent):String
			{
				return "asBindFun" + Math.random().toString();
			}
			
			private function bindFunClickHandler(event:MouseEvent):void
			{
				dispatchEvent(new Event("customEvent"));
				
			}
			
			[Bindable]
			public function get getterFun():String
			{
				return "getterFun" + Math.random().toString();
			}
			
			public function set getterFun(value:String):void
			{
				
			}
			
			private function getterFunClickHandler(event:MouseEvent):void
			{
				getterFun = "some string" + Math.random().toString();
				
			}
			
			private function bindComplexObjectClickHandler(event:MouseEvent):void
			{
				person.name = "kenny"+ Math.random().toString();
			}
			
			protected function bindComplexObjectComplexAttClickHandler(event:MouseEvent):void
			{
				person.company.name = "lomboard"+ Math.random().toString();
				
			}
			
			protected function bindPropertyClickHandler(event:MouseEvent):void
			{
				//BindingUtils.bindProperty(baseBindInput,"text",this,"asBindInputStr");//绑定基本数据类型,属性都必须为public
				//BindingUtils.bindProperty(baseBindInput,"text",this,["person","company","name"]);//绑定复杂数据类型,属性都必须为public
				//BindingUtils.bindProperty(baseBindInput,"text",this,"getterFun");//绑定get,set方法,属性都必须为public
				BindingUtils.bindProperty(baseBindInput,"text",this,{name:"asBindInputStr",getter:asBindFun});
				/* 绑定方法,最后这个objec参数是强制的格式,必须还有name和getter属性,name的值必须为host的一个public属性,getter
				方法必须为host的一个pulic方法,并且必须含有参数,次参数代表host。 */
			}
		]]>
	</fx:Script>
	<fx:Declarations>
		
	</fx:Declarations>
	<s:TextInput text="{inputStr}"/><!--绑定-->
	<s:TextInput text="@{inputStr}"/><!--双向绑定-->
	<s:Button click="changInputStrClickHandler(event)" label="base One"/>
	<s:TextInput id="sourceInput"/>
	<s:TextInput id="destinationInput"/>
	<fx:Binding source="sourceInput.text" destination="destinationInput.text" twoWay="true"/>
	<!--twoWay 属性标志是否是双向绑定-->
	<s:Label text="{bindFun()}" click="bindFunClickHandler(event)"/>
	<!--绑定到自定义函数中时,你必须自定义绑定的事件,不然不会绑定-->
	<s:Label text="{getterFun}" click="getterFunClickHandler(event)"/>
	<!--绑定到get set函数中时,你必须保证get set方法配对,然后当调用set方法时,flex就会自动触发PropertyChangeEvent事件-->
	<s:Label text="{person.name}" click="bindComplexObjectClickHandler(event)"/>
	<!--对于复杂类型的绑定,如果你只想绑定person的属性,那么那个属性必须加上[Bindable]标签,person对象不设置[Bindable]标签也可以,只不过有一个warning而已。-->
	<s:Label text="{person.company.name}" click="bindComplexObjectComplexAttClickHandler(event)"/>
	<!--如果你想绑定复杂类型的复杂属性下面的属性,那么这个复杂属性也必须的加上[Bindable]标签。也就是说整个绑定链都得是可以绑定的。-->
	<s:Button label="BindingUtils bindProperty" click="bindPropertyClickHandler(event)"/>
	<s:TextInput id="baseBindInput" />
</s:Application>

 Person:

package com.mode.clone
{
	public class Person
	{
		private var _name:String;
		[Bindable]
		public var company:Company;//另一个自定义对象
		[Bindable]
		public function get name():String
		{
			return _name;
		}

		public function set name(value:String):void
		{
			_name = value;
		}
	}
}

 Company:

package com.mode.clone
{
	public class Company
	{
		[Bindable]
		public var name:String;
	}
}

 

分享到:
评论

相关推荐

    Flex 动态绑定BindingUtils.bindProperty

    `BindingUtils.bindProperty`是Adobe Flex中用于实现这种动态绑定的关键方法。这个方法允许开发者在运行时创建数据绑定,而无需在MXML中硬编码。 `BindingUtils.bindProperty`方法接收四个参数: 1. `site`: 这是...

    Flex Data Binding详解

    在Flex中,数据绑定确保当数据源发生变化时,相关的用户界面元素能够自动更新,反之亦然。 **数据绑定何时发生** 1. **属性值改变时**:当绑定源的属性值发生更改时,Flex会检测到这种变化并自动更新目标属性。 2....

    FLEX内存释放优化原则

    - 如果使用了记录功能(如`BindingUtils.bindSetter()`或`ChangeWatcher.watch()`),那么在不再需要这些功能时,应当调用相应的取消记录方法(如`ChangeWatcher.unwatch()`)。这可以避免因记录未被清除而导致的...

    flex如何进行内存优化

    - 当使用`BindingUtils.bindSetter()`或`ChangeWatcher.watch()`等方法时,记得调用`ChangeWatcher.unwatch()`来取消监听,以防止内存泄露。 3. **效果处理:** - 当使用动画效果(如`Effect`)时,在不再需要时...

    FLEX内存优化技巧集合

    2. **系统类泄露**:使用系统类如BindingUtils、ChangeWatcher等后,忘记执行相应的清除操作。 3. **效果泄露**:应用效果到组件后,删除对象前需停止效果并解除target引用。 4. **SWF泄露**:卸载SWF时,调用unload...

Global site tag (gtag.js) - Google Analytics