as3 非flex项目实现数据绑定

来自: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,继续工作

转载于:https://www.cnblogs.com/apaqi/archive/2012/06/10/2544351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值