`

Flex [Bindable] 以及使用方法

    博客分类:
  • flex
阅读更多
绑定:

举个例子: 给下面的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为用户隐藏了很多底层算法。
分享到:
评论

相关推荐

    Flex Bindable 的用法

    什么是元数据(metadata):[Bindable]大概又是Flex用得最多的元数据了。

    FLEX企业应用开发实战.part1

     2.4.5 使用Bindable元数据标记  2.4.6 绑定到函数、对象和数组  2.4.7 使用可绑定属性链进行工作  2.5 Flex中的组件化编程  2.5.1 认识Flex组件和组件容器  2.5.2 组件生命周期与布局  2.5.3 组件的...

    FLEX企业应用开发实战.part2

     2.4.5 使用Bindable元数据标记  2.4.6 绑定到函数、对象和数组  2.4.7 使用可绑定属性链进行工作  2.5 Flex中的组件化编程  2.5.1 认识Flex组件和组件容器  2.5.2 组件生命周期与布局  2.5.3 组件的...

    Flex企业应用开发实战源代码

    再接着详细讲解了BlazeDS框架的使用方法和工作原理,并通过迭代的方式完整地演示一个真实的Flex企业级应用的开发全过程,实战性极强;最后重点探讨Flex应用性能优化等方面的高级知识。值得一提的是,本书公开了作者...

    flex导出excel的代码

    [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("行的数据分别是...

    Flex调用xml通过DataGrid遍历简单示例

    [Bindable] private var slides:ArrayCollection private function resultHandler(event:ResultEvent):void { slides = event.result.album.slide; } private function faultHandler...

    flex 绑定元数据

    算是flex初学者的的笔记吧,首先可以概览一下下文中的代码,然后我们主要是看看[Bindable]的这种用法。

    flex实列demo

    *" creationComplete="initTree();... change="iFrame.source=tree.selectedNode.path" /&gt; ... // TODO: determine whether we can ... // TODO: add some links here to Flex sites } ]]&gt; &lt;/mx:Application&gt;

    flexjava交互

    [Bindable] var result:String; function sendRequest(){ var name=name1.text; var password=password1.text; ro.validateLogin(name,password); ro.addEventListener(ResultEvent.RESULT,results); } function ...

    open flash chart 2 swc包

    open flash chart 2 的swc文件包 可以在flex里面调用了. [Bindable] private var chart:String='{ "elements": [ { "type": "bar","alpha":0.8,"colour":"#663366", "values": [ 0 ] } ], "title": { "text": "...

Global site tag (gtag.js) - Google Analytics