[转]Flex中[Bindable]的用法
- 博客分类:
- 技术杂绘
[转]Flex中[Bindable]的用法
2010年06月02日
什么是元数据(metadata):[Bindable]大概又是Flex用得最多的元数据了。我就按自己的理解随便解释一下:首先要明白元数据不是语法的一部分,而是专门给编译器用的
,说白了是告诉编译器做某些事情,学过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也跟着自动变化了,爽吧。
能用在哪里?三个地方:类, 变量。
1、getter/setter。是不是public没有关系,private的就只能给自家用呗。
2、Class。就是简单的给类所有的public属性(包括变量,getter/setter,普通方法)加上 [Bindable],可是一般的方法不能用[Bindable]呀,于是一般就能看到flex给了
个warning,直接无视。变量嘛就是上面讲的,很简单略掉。
3、用在只读,只写属性(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绑定_wholeText,可它是不工作的。为什么?_wholeText太复杂了,被编译器排除在"可能"之外,编译器认为没有绑定关系,如果只是简单的"return
content"倒是可以的。我这里搜到了一些比较权威的解释。来自http://www.rubenswieringa.com/blog/binding-read-on ly-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被赋值,发出_contentChanged事件,通知所有被绑定的getter方法执行一遍。这也说明了,绑定不过是事件游戏而已,flex为用户隐藏了很多底层算法。
发表评论
-
Flex学习笔记(1)
2012-01-20 00:35 536Flex学习笔记(1) 2010年06 ... -
从头开始编写操作系统(8) 第7章:系统结构
2012-01-20 00:35 422从头开始编写操作系统( ... -
Gartner Business Intelligence 08年Gartner商务智能平台魔法四分区
2012-01-20 00:35 677Gartner Business Intelligence 0 ... -
Linux 2.6 版内核引导过程学习
2012-01-20 00:35 885Linux 2.6 版内核引导过程学习 2010年12月29 ... -
杨建:网站加速--服务器编写篇(上)
2012-01-17 00:54 467杨建:网站加速--服务器 ... -
关于J2EE中死锁问题的研究
2012-01-17 00:53 541关于J2EE中死锁问题的研 ... -
file_operations
2012-01-17 00:53 584file_operations 2011年09月19 ... -
ARM经典100问
2012-01-17 00:53 798ARM经典100问 2011年03月13日 第1章 体系 ... -
转:V4L2 - Linux下视频驱动模型
2012-01-17 00:53 964转:V4L2 - Linux下视频驱动模型 2011年03月 ... -
linux C库函数(一)
2012-01-15 19:38 659linux C库函数(一) 2010年0 ... -
系统调用、POSIX、C库、系统命令和内核函数
2012-01-15 19:38 585系统调用、POSIX、C库、 ... -
socket性能优化(上)
2012-01-15 19:38 584socket性能优化(上) 2012 ... -
诬凹沃官燠喷闳
2012-01-15 19:38 578诬凹沃官燠喷闳 2012年01月06日 U辙趴“ ... -
是傻频日志呵阡啄凛仄
2012-01-15 19:38 488是傻频日志呵阡啄凛仄 2012年01月09日 刘 ...
相关推荐
什么是元数据(metadata):[Bindable]大概又是Flex用得最多的元数据了。
2.5 Flex中的组件化编程 2.5.1 认识Flex组件和组件容器 2.5.2 组件生命周期与布局 2.5.3 组件的失效机制 2.5.4 使用ActionScript创建自定义组件 2.6 异步调用 2.6.1 异步调用导致模型数据不一致...
2.5 Flex中的组件化编程 2.5.1 认识Flex组件和组件容器 2.5.2 组件生命周期与布局 2.5.3 组件的失效机制 2.5.4 使用ActionScript创建自定义组件 2.6 异步调用 2.6.1 异步调用导致模型数据不一致...
再接着详细讲解了BlazeDS框架的使用方法和工作原理,并通过迭代的方式完整地演示一个真实的Flex企业级应用的开发全过程,实战性极强;最后重点探讨Flex应用性能优化等方面的高级知识。值得一提的是,本书公开了作者...
[Bindable] private var dp:Array = [ {idx:1, names: "test1", sex: "b" }, {idx:2, names: "test2", sex: "g" } ]; public function doSelect(o:Object):void { Alert.show("行的数据分别是...
Bindable.js 实现了灵活、快速的双向数据绑定的 JavaScript 库。 Two-way data binding means linking properties of two separate objects - when one changes, the other will automatically update with that ...
[Bindable] private var slides:ArrayCollection private function resultHandler(event:ResultEvent):void { slides = event.result.album.slide; } private function faultHandler...
Bindable是一个设计系统,旨在在工具中提供视频产品,以构建紧密一致的界面。 Bindable将为设计者和工程师提供一种通用的模式。 Bindable是作为插件开发的,并使用构建。 目录 背景 我们是由设计师和工程师组成的...
算是flex初学者的的笔记吧,首先可以概览一下下文中的代码,然后我们主要是看看[Bindable]的这种用法。
本文主要讲解AngularJS ng-non-bindable 指令,这里帮大家整理了ng-non-bindable指令的基本知识资料,有需要的小伙伴可以参考下
*" creationComplete="initTree();... change="iFrame.source=tree.selectedNode.path" /> ... // TODO: determine whether we can ... // TODO: add some links here to Flex sites } ]]> </mx:Application>
[Bindable] var result:String; function sendRequest(){ var name=name1.text; var password=password1.text; ro.validateLogin(name,password); ro.addEventListener(ResultEvent.RESULT,results); } function ...
可结合成分 公开BindableComponent以轻松绑定组件方法以用作react中的处理程序。 还提供了BaseComponent ,唯一的区别是包含了PureRenderMixin 。安装npm install --save bindable-component用法import { ...
var BindableObject = require ( "bindable-object" ) ; var person = new BindableObject ( { name : "craig" , last : "condon" , location : { city : "San Francisco" } } ) ; person . bind ( "location....
如何使用依赖属性通过将DependencyPropertyAttribute应用于单个属性或整个类,可以将常规属性转换为依赖属性。 当您将属性应用于类时,所有可用属性都将转换为依赖项属性。 不会转换的属性是: 非汽车属性。 (具有...
open flash chart 2 的swc文件包 可以在flex里面调用了. [Bindable] private var chart:String='{ "elements": [ { "type": "bar","alpha":0.8,"colour":"#663366", "values": [ 0 ] } ], "title": { "text": "...
木偶的可绑定路径参数此路由器允许您使用前导@char 绑定控制器方法以路由参数。 路由器示例: appRoutes: { 'users/@user/edit' : 'onUserEdit' , 'users/@user/copy' : 'onUserCopy' , '*default' : 'onDefault' } ...
提供使用流利的语法创建自定义@observable功能: @observable.custom / @observable.custom() 提供4个基本的自定义可观察对象: @observable.date , @observable.number , @observable.string , @observable....