当前位置: 首页 > news >正文

深入分析Flex [Bindable] 以及使用方法

[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/blog/binding-read-only-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为用户隐藏了很多底层算法。

相关文章:

  • indy10 学习2
  • 一个网页设计需求方眼中的网页设计
  • 正则表达式入门以及记录
  • 谈谈网页设计师的成长之路
  • 07.常用的SQL语句
  • 初步接触html心得
  • AS3所有类的接口实现一览表
  • 分页查询中绑定变量
  • Oracle11g字符集AL32UTF8修改为ZHS16GBK详解
  • 我参加了“2010年度十大杰出IT博客”大赛,请朋友们支持!
  • 使用jQuery开发一个响应式超酷整合RSS信息阅读杂志
  • MeeGo handset 1.1开发环境[6]:封装*.src.rpm和*.rpm
  • 使用Windows Service Wrapper快速创建一个Windows Service
  • [IE9] 解决了傲游、搜狗浏览器在IE9下网页截图的问题
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • Docker容器管理
  • mac修复ab及siege安装
  • MaxCompute访问TableStore(OTS) 数据
  • nodejs:开发并发布一个nodejs包
  • Node项目之评分系统(二)- 数据库设计
  • orm2 中文文档 3.1 模型属性
  • PaddlePaddle-GitHub的正确打开姿势
  • spring-boot List转Page
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 前端临床手札——文件上传
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​secrets --- 生成管理密码的安全随机数​
  • ​如何防止网络攻击?
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • # 达梦数据库知识点
  • #pragma data_seg 共享数据区(转)
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (2022 CVPR) Unbiased Teacher v2
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (搬运以学习)flask 上下文的实现
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (九十四)函数和二维数组
  • (四)库存超卖案例实战——优化redis分布式锁
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET Core中Emit的使用
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .net MySql
  • .Net Web窗口页属性
  • .NET 读取 JSON格式的数据
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .NET轻量级ORM组件Dapper葵花宝典
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • @Bean, @Component, @Configuration简析