# Bindable Flex中Bindable的原理

[Bindable]        private var bind_String:String="hi";

[Bindable(event="propertyChange")]
private var bind_String:String="hi";

private function onChange():void
{
var oldValue:String = bind_String;
bind_String = "hello";
if(bind_String!==oldValue) {
this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,
"bind_String", oldValue, bind_String));
}
}

[Bindable]
private var content:Array = new Array();
[Bindable]
public function set _content(ct:String):void
{
content = ct.split(SEP);
}
[Bindable]
public function get _wholeText():String
{
if(content.length == 0)
{
return "";
}
else
{
var _w:String = "";
for(var i:int=0 ; i<content.length ; i++)
{
_w += content + "\r\n";
}
return _w;
}
}

Now keep in mind that there’s no way for the compiler to actually tell if the value of a property get function would be different if called, short of doing an extensive code flow analysis of the get function, identifying all the inputs that might be affecting the value of the get function (i.e., member fields, statics, globals that are used in the get function and in any methods, global functions, closures, etc) it might call, and setting up watchers on every one of those to trigger the binding when any of them change. That’s prohibitively difficult, and expensive to do. So the compiler doesn’t try.

Instead when you put [Bindable] on a get/set property, the compiler makes it bindable with a little creative rewriting that allows the framework to watch the get function, and dispatch a change event when the get function is triggered. This means that automatic bindable properties don’t work when the get function is computed from multiple values, or when you change its value by setting a backing field, rather than using the set function.

It _also_ means that if you have no set function, we can pretty much guarantee that there’s no way automatically bindable get properties will be triggered. a read only propeerty is, to the compiler, completely opaque…at the moment, it has no idea where that value is coming from, and hence will never be able to ‘automatically’ trigger the binding.

[Bindable]
private var content:Array = new Array();
[Bindable]
public function set _content(ct:String):void
{
content = ct.split(SEP);
this.dispatchEvent(new Event("_contectChanged"));
}
[Bindable("_contectChanged")]
public function get _wholeText():String
{
if(content.length == 0)
{
return "";
}
else
{
var _w:String = "";
for(var i:int=0 ; i<content.length ; i++)
{
_w += content + "\r\n";
}
return _w;
}
}

http://blog.csdn.net/lixuekun820/article/details/5492697 经典

• 本文已收录于以下专栏：

## Flex中[Bindable]的使用心得

Bindable的实现采用了GoF的Observer模式，或者Publisher/Subscriber模式。该实现允许一个类(或者变量)可以将自身的变化自动通知给感兴趣的对象。

## Flex 学习总结 -- Bindable的原理

[Bindable]：元数据标签，它在代码中的作用就是向编译器提供如何编译程序的信息。它的最大作用是使程序组件间的数据同步变得容易。在开发中通常用上Bindable作用在视图控件上，如给它绑定一个对象...

## Flex中Event与Bindable

Event: 1.事件派发 Dispatching Flash.events.EventDispacher类提供了事件dispatch函数,如果需要dispatch事件,则应该考虑继承该类或其子类...

## Flex中的Bindable等元数据标签的用法

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)