Flex 加bindable标签后的源码分析
sshong 发表于2013年2月6日 18:10:30 更新于2013年2月6日 18:12:32
Bindable的原理很简单:数据改变时触发event。
我自己测试了下几种不同bindable,编译后的源码。

几个测试:
1、一个非继承自EventDispatcher的类来加Bindable

package
{
    [Bindable]
    public class TestBind
    {
        public var prop:String;
    }
}
转化为:
package
{
    import flash.events.*;
    import mx.events.*;

    public class TestBind extends Object implements IEventDispatcher
    {
        private var _bindingEventDispatcher:EventDispatcher;
        private var _3449699prop:String;

        public function TestBind()
        {
            _bindingEventDispatcher = new EventDispatcher(IEventDispatcher(this));
            return;
        }// end function

        public function dispatchEvent(event:Event) : Boolean
        {
            return _bindingEventDispatcher.dispatchEvent(event);
        }// end function

        public function get prop() : String
        {
            return this._3449699prop;
        }// end function

        public function set prop(param1:String) : void
        {
            var _loc_2:* = this._3449699prop;
            if (_loc_2 !== param1)
            {
                this._3449699prop = param1;
                this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "prop", _loc_2, param1));
            }
            return;
        }// end function

        public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            _bindingEventDispatcher.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public function hasEventListener(param1:String) : Boolean
        {
            return _bindingEventDispatcher.hasEventListener(param1);
        }// end function

        public function willTrigger(param1:String) : Boolean
        {
            return _bindingEventDispatcher.willTrigger(param1);
        }// end function

        public function addEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            _bindingEventDispatcher.addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

    }
}

2、一个继承自EventDispatcher的类来加Bindable

package
{
    import flash.events.EventDispatcher;

    [Bindable]
    public class TestBind extends EventDispatcher
    {
        public var prop:String;
    }
}
转化为
package
{
    import flash.events.*;
    import mx.events.*;

    public class TestBind extends EventDispatcher
    {
        private var _3449699prop:String;

        public function TestBind()
        {
            return;
        }// end function

        public function get prop() : String
        {
            return this._3449699prop;
        }// end function

        public function set prop(param1:String) : void
        {
            var _loc_2:* = this._3449699prop;
            if (_loc_2 !== param1)
            {
                this._3449699prop = param1;
                this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "prop", _loc_2, param1));
            }
            return;
        }// end function

    }
}

3、自定义bind 事件和不自定义bind事件
package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
   
    [Bindable]
    public class TestBind extends EventDispatcher
    {
        private var _prop:String;
       
        [Bindable("propChanged")]
        public function get prop():String
        {
            return _prop;
        }
        public function set prop(value:String):void
        {
            _prop = value;
            dispatchEvent(new Event("propChanged"));
        }
       
        public var prop2:String;
    }
}
转化为
package
{
    import flash.events.*;
    import mx.events.*;

    public class TestBind extends EventDispatcher
    {
        private var _prop:String;
        private var _106940719prop2:String;

        public function TestBind()
        {
            return;
        }// end function

        public function get prop() : String
        {
            return _prop;
        }// end function

        public function set prop(param1:String) : void
        {
            _prop = param1;
            dispatchEvent(new Event("propChanged"));
            return;
        }// end function

        public function set prop2(param1:String) : void
        {
            var _loc_2:* = this._106940719prop2;
            if (_loc_2 !== param1)
            {
                this._106940719prop2 = param1;
                this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "prop2", _loc_2, param1));
            }
            return;
        }// end function

        public function get prop2() : String
        {
            return this._106940719prop2;
        }// end function

    }
}
标签:绑定bind分类:As3&Flex阅读:1428
评论
暂无评论
添加评论
您的大名,限长10汉字,20英文(*)
电子信箱(*)
您的网站
正文,限长500汉字,1000英文(*)
验证码(*) 单击刷新验证码
联系我
博客订阅