[Bindable]大概又是Flex用得最多的元数据了。刚开始用用确实好简单,效率真是没的说。不过这几天用着却碰到了些问题,我自己搜集了些资料,想着有必要在blog里总结一下吧。

啥是元数据(metadata)

知道就跳过吧。今天不晓得为什么livedoc.adobe.com这么慢,没办法,拿不到权威的解释了。我就按自己的理解随便解释一下:首先要明白元数
据不是语法的一部分,而是专门给编译器用的,说白了是告诉编译器做某些事情,学过java之类的应该知道。那Bindable来讲,它的作用是告诉
flex编译器,给某些某些东西建立绑定关系,flex编译器会在编译过程中给AS(flex编译器就是把mxml编译成as,再编译到swf,也可能直
接编译倒swf,我这里假设有as这么个环节)加一点事件发生和处理之类的代码,由此绑定的关系便建立了,如果我们用纯粹as3代码来写也是可以实现的,
就是太太太麻烦。

啥是绑定

知道继续跳过。举个例子:给下面的public变量加上[Bindable]

[Bindable]
public var name:String = "";

作为一个public变量,肯定既可以被赋值,也能赋值给别的变量。绑定的作用就是,当name改变的时候(被赋值了),可能通知其它被name影响(赋
值给它们)的变量发生改变。这里的“可能”就需要编译器来判断,这就是为什么元数据是给编译器用的原因了。在mxml里用{}的语法的地方就是绑定的对
象,比如label={xxx.name},当name变化,label也跟着变化。这样,我们只是很简单的改变了name的值,由于有绑定,界面上的
label也跟着自动变化了,爽吧。

能用在哪里

三个地方:类, 变量,
getter/setter。是不是public没有关系,private的就只能给自家用呗。用在Class上就是简单的给所有的public属性(包
括变量,getter/setter,普通方法)加上[Bindable],可是一般的方法不能用[Bindable]呀,于是一般就能看到flex给了
个warning,直接无视。变量嘛就是上面讲的,很简单略掉。

用在只读,只写属性(getter/setter)上面

终于讲到关键地方了,因为getter和setter很像方法,用起来会有点不同。看看这个例子:

[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[i] + "\r\n";
                }
                return _w;
        }
}

原来的设想是content绑定_wholeText,可它是不工作的。为什么?_wholeText太复杂了,被编译器排除在“可能”之外,编译器认为没有绑定关系,如果只是简单的return content,倒是可以的。我这里搜到了一些比较权威的解释。来自http://www.rubenswieringa.com/bl ... y-accessors-in-flex找到Ely Greenfield讲的。

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.

说白了就是为了降低复杂度和提高效率,复杂情况的getter会被忽略。如何解决?可以手动建立绑定,即[Bindable("eventName")]。把代码改成这样:

[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[i] + "\r\n";
                }
                return _w;
        }
}

这样就避免了编译器去自动识别。自己加上绑定关系,当_content被赋值,发出_contentChanged事件,通知所有被绑定的getter方法执行一遍。这也说明了,绑定不过是事件游戏而已,flex为用户隐藏了很多底层算法。

flex Bindable的更多相关文章

  1. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十三)台风模块

    config.xml文件的配置如下: <widget label="台风" icon="assets/images/typhoon.png" config ...

  2. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十一)路径导航模块

    config.xml文件的配置如下: <widget label="路径导航" icon="assets/images/lujingdaohang.png" ...

  3. arcgis api for flex之专题图制作(饼状图,柱状图等)

    最近公司给我一个任务,就是利用arcgis api for flex实现在地图上点(业务数据)直接显示饼状图以及柱状图的专题图制作,而不是通过点击点显示气泡窗口的形式来实现,这个公司已经实现了. 经过 ...

  4. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块

    config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...

  5. Flex数据交互之Remoting

    一 前言 Flex数据交互常用的有三种方式:WebService.HttpService以及Remoting. WebService方式已在这篇文章中给出,这篇文章主要讲解以Remoting方式进行数 ...

  6. 利用Flex组件birdeye绘制拓扑关系图

    birdeye绘制拓扑关系图 1.flex简单介绍 Flex 是一个高效.免费的开源框架,可用于构建具有表现力的 Web应用程序,这些应用程序利用Adobe Flash Player和Adobe AI ...

  7. Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据

    http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...

  8. Flex数据绑定陷阱(一)

    Flex数据绑定陷阱:常见的误用和错误 当构建Flex或者Adobe AIR程序时,将一个对象的值自动的传递给另一个对象这种处理是数据绑定最常 用并最有用的特征之一. 尽管如此,同时数据绑定会减缓程序 ...

  9. Flex入门笔记

    Test_01.mxml <?xml version="1.0" encoding="utf-8"?> <viewer:BaseWidget  ...

随机推荐

  1. 转载------------------关于android的一些技巧

    Android eclipse中程序调试 一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的.步骤如下.1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键点击 ...

  2. 【Python】可变对象和不可变对象

    Python在heap中分配的对象分成两类:可变对象和不可变对象.所谓可变对象是指,对象的内容是可变的,例如list.而不可变的对象则相反,表示其内容不可变. 不可变对象:int,string,flo ...

  3. 设计模式之观察者模式(Observer)

    观察者模式原理:当有新的消息产生时发送给观察者,和中介者模式的不同地方是中介者模式强调中介的作用以及中介双方的交互,观察者模式是主动调用观察者成员函数进行消息发送. 代码如下: #include &l ...

  4. UVA 10078 The Art Gallery

    Problem: Century Arts has hundreds of art galleries scattered all around the country and you are hir ...

  5. 关灯问题 dp

    题意是一排路灯,每个路灯有耗电量,照明度,需要给这n个路灯按顺序分组,每组内的最大耗电量是电灯数乘t,可以选择关闭一些电灯,求最大的照明度: 这题思路很明显,预处理出一个g[i][j]表示i到j分为一 ...

  6. Codeforces Round #268 (Div. 2)

    补题解: E:只会第四种解法:也只看懂了这一种. PS:F[X+10^18]=F[X]+1;F[X]表示X的数字之和; 假设X,F[10^18+X]+F[10^18+X-1]+......F[10^1 ...

  7. Sqli-labs less 59

    Less-59 与less58一致,直接给出一个示例payload: http://127.0.0.1/sqli-labs/Less-59/?id=-1 union select extractval ...

  8. Sublime Text 编辑器

    1.从http://www.sublimetext.com/2 下载Sublime Text 2编辑器. 2.安装Package Control 管理器,用于管理和安装插件包. 下载地址:https: ...

  9. iOS多线程的初步研究(六)-- NSOperation

    iOS平台提供更高级的并发(异步)调用接口,让你可以集中精力去设计需完成的任务代码,避免去写与程序逻辑无关的线程生成.运行等管理代码.当然实质上是这些接口隐含生成线程和管理线程的运行,从而更加简洁地实 ...

  10. 如何在Windows7(IIS7)环境下安装 PHP

    一.安装IIS7 打开(1)[程序和功能],然后点击(2)[打开或关闭Windows功能] 勾选(1)[IIS管理控制台]和(2)CGI,然后点击[确定]按钮,等待安装完成.这个过程可能需要系统安装光 ...