来自:http://hi.baidu.com/langhua292/blog/item/629882354e2ea9aba71e12ff.html
最近开发的项目数据量超多,Model的每一项发生了变化都要发一个消息通知View层去更新,超级烦琐
不过懒人有懒办法,刚刚写了一个类似flex里[Binding]的功能的方法,与大家分享一下,懒得打字,就不做解释了,直接看代码注释吧。
package com.langhua.binder
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
/**
* 数据对象通过继承此类来实现与显示对象的数据绑定
* 此数据对象在定义可绑定属性的时候,应该用相应的setter和getter方法,并且应在setter方法中调用dispatchPropertyEvent方法
* @author langhuayiduoduo
*
*/
public class BaseValueObject extends EventDispatcher
{
public function BaseValueObject()
{
super(null);
}
/**
* 派发属性改变事件
* @param property 应为定义的属性名
* @return void
*
*/
protected function dispatchPropertyEvent(property:String):void
{
this.dispatchEvent(new Event(property));
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
package com.langhua.binder
{
/**
* 通过此类实现数据绑定
* @author langhua
*
*/
public class Binder
{
public function Binder()
{
}
/**
* 将指定的数据对象(data)的指定属性(dataProperty)与指定的显示对象(displayObj)的指定属性(displayObjProperty)绑定
* @param data
* @param dataProperty
* @param displayObj
* @param displayObjProperty
*
*/
public static function bind(data:BaseValueObject,dataProperty:String, displayObj:*,displayObjProperty:String):void
{
if( !data.hasOwnProperty(dataProperty) )
{
throw new Error("has no property --- " + "\'" + dataProperty + "\' in " + data, "#BindingError");
}
if( !displayObj.hasOwnProperty(displayObjProperty) )
{
throw new Error("has no property --- " + "\'" + displayObjProperty + "\' in " + displayObj, "#BindingError");
}
displayObj[displayObjProperty] = data[dataProperty];
data.addEventListener
(
dataProperty,
function ():void
{
displayObj[displayObjProperty] = data[dataProperty];
}
);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
package com.langhua.specificValueObject
{
import com.langhua.binder.BaseValueObject;
/**
* 具体的数据对象
* @author langhuayiduoduo
*
*/
public class SpecificVO extends BaseValueObject
{
public function SpecificVO()
{
super();
}
private var _clickTimes:uint;
/** 单击舞台的次数 */
public function get clickTimes():uint
{
return this._clickTimes;
}
public function set clickTimes(value:uint):void
{
this._clickTimes = value;
this.dispatchPropertyEvent("clickTimes");
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
最后是文档类
package
{
import com.langhua.binder.Binder;
import com.langhua.specificValueObject.SpecificVO;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
public class DataBinding extends Sprite
{
private var vo:SpecificVO;//数据对象
private var txt:TextField;//显示对象
public function DataBinding()
{
vo = new SpecificVO;
txt = new TextField();
txt.x = 100;
txt.y = 100;
txt.selectable = false;
this.addChild(txt);
Binder.bind(vo,"clickTimes",txt,"text");
stage.addEventListener(MouseEvent.CLICK,onClickStage);
}
private function onClickStage(e:MouseEvent):void
{
vo.clickTimes ++;
}
}
}
这样写其实model层写起来会有些麻烦,没增加一个属性都要使用getter和setter方法,但相对于在View层写一大堆addEventListener,我相信你更愿意用前者,OK,继续工作