`
maloveqiao
  • 浏览: 102699 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Flex 绑定/双向绑定

    博客分类:
  • flex
 
阅读更多
1.什么是数据绑定?

   Data binding is the process of tying the data in one object to another object.

   数据绑定是一个对象的数据捆绑到另一个对象上的进程。

2.数据绑定的作用

   It provides a convenient way to pass data between the different layers of the application.

   数据绑定提供了一个在程序的各个层面之间传递数据的便利方法

3.数据绑定的机制

   Data binding requires a source property, a destination property, and a triggering
event that indicates when to copy the data from the source to the destination. An
object dispatches the triggering event when the source property changes.

   数据绑定要求一个源属性,一个目的属性和一个触发事件。这个触发事件指示从源属性复制数据到目的属性上。当源属性改变时一个对象派发这个触发事件。

4.数据绑定的定义方式

   1)句法:大括号{}

   2)MXML标签:<mx:Binding>

   3)AS类: mx.binding.utils.BindingUtils

5.数据绑定的发生时机

   1) The binding source dispatches an event because the source has been modified.

       绑定源改变后派发事件

       This event can occur at any time during application execution.

        这个事件在程序运行的任何时间都可发生。

    2)At application startup when the source object dispatches the initialize event.

         程序启动时源对象派发初始化事件

         All data bindings are triggered once at application startup to initialize the

         所有的数据绑定在程序启动初始化目的属性时都会被触发一次。

6.绑定属性

1)Using data binding with data models

       绑定数据model

   2)  Binding a source property to more than one destination property

        一对多绑定

   3)Binding more than one source property to a destination property

        多对一绑定

   4)Defining bidirectional bindings

        双向绑定

        eg:

7.绑定函数

1)Using functions that take bindable properties as arguments

    2)  Binding to functions in response to a data-binding event

8.绑定对象

1)  Binding to Objects

     2)  Binding to properties of Objects

          绑定对象的属性,可使用元素标签[Bindable]在对象所在的类的定义前。则这个对象的所有

公有属性都被绑定。

9.绑定数组

1)  Binding to arrays

     2)  Binding to array elements

10.绑定元素标签

句法:[Bindable] 或 [Bindable(event="eventname")]

[Bindable]是[Bindable(event="propertyChange")]的简写。当被绑定的属性发生改变时,Flex

会派发事件触发绑定。

主要有三种使用情况:

1) 在属性定义前 2) 类定义前 3)getter 或 setter 方法前



可以使用MX标签绑定,也可以使用BindUtil来进行绑定,下面是我做的一个例子,实现BO与页面空间的自动双向绑定:

BO定义:


package com.bankcomm.icms.domain.migrate

{

    [Bindable]

    public class Bo {

        private var _property0:String = "";

        private var _property1:int = 0;

        private var _property2:String = "";

       

        public var className:String = "Bo";

        public var propertyNames:Array = ["property0", "property1", "property2"];

        public function Bo() {}

       



        public function set property0(value:String):void{

            this._property0 = value;

        }



        public function get property0():String{

            return this._property0;

        }



        public function set property1(value:int):void{

            this._property1 = value;

        }

       

        public function get property1():int{

            return this._property1;

        }



        public function set property2(value:String):void{

            this._property2 = value;

        }

       

        public function get property2():String{

            return this._property2;

        }       

    }

}




<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">

    <mx:Script>

        <![CDATA[

            import com.bankcomm.icms.domain.migrate.Bo;

            import com.bankcomm.icms.common.DataDict;

            import mx.binding.utils.BindingUtils;

            import mx.collections.ArrayCollection;

           

            private var bo:Bo = new Bo();

           

            function init():void {

                autowireBindObject(bo);

            }

           

            function autowireBindCollection(arr:ArrayCollection):void {

               

            }

           

            function autowireBindObject(bo:Object) {

                for each(var propertyName:String in bo.propertyNames) {

                    var elem:Object = this[bo.className + "_" + propertyName];

                    if(elem==null) continue;           

                    var value = bo[propertyName];

                   

                    if(elem instanceof ComboBox) {

                        ComboBox(elem).dataProvider = DataDict.dictYesNo;

                        BindingUtils.bindProperty(elem, "selectedIndex", bo, propertyName, false);

                        BindingUtils.bindProperty(bo, propertyName, ComboBox(elem), "selectedIndex",false);

                    } else if(elem instanceof TextInput) {

                        BindingUtils.bindProperty(elem, "text", bo, propertyName, false);

                        BindingUtils.bindProperty(bo, propertyName, TextInput(elem), "text", false);

                    } else if(elem instanceof DateField) {

                        BindingUtils.bindProperty(elem, "text", bo, propertyName, false);

                        BindingUtils.bindProperty(bo, propertyName, DateField(elem), "text", false);

                    } else {

                       

                    }

                }

            }

           

            function chageModelAndUpdateUI() {

                bo.property0 = "xxx";

                bo.property1 = 1;

                bo.property2 = "2009-02-10";

            }

           

            function chageUIAndUpdateModel():void {

                var a = bo.property1;

            }

           

        ]]>

    </mx:Script>

   

    <mx:TextInput id="Bo_property0" x="65" y="10"/>

    <mx:ComboBox id="Bo_property1" x="65" y="51" width="160"/>

    <mx:DateField id="Bo_property2" x="65" y="92" width="160"/>

   

    <mx:Button x="65" y="133" label="模型改变更新UI" click="chageModelAndUpdateUI();" />

   

    <mx:Button x="179" y="133" label="UI改变更新模型" click="chageUIAndUpdateModel();" />

   

   

   

</mx:Application>

分享到:
评论

相关推荐

    FLEX资源——FLEX数据绑定专题(中文PDF)

    3. **双向数据绑定**:双向数据绑定使得UI组件的改变可以立即反映到数据模型中,反之亦然。这对于实现动态表单和实时更新的界面特别有用。 4. **事件驱动的数据绑定**:当数据模型发生变化时,Flex会触发相应的事件...

    flex数据绑定的原理

    5. **双向数据绑定**:Flex不仅支持单向数据绑定(从数据模型到UI),还支持双向数据绑定。这意味着UI组件的改变不仅能更新数据源,数据源的更新也能反映回UI。 6. **性能优化**:Flex数据绑定系统还包含了一些性能...

    FLEX数据绑定四种方式

    `&lt;mx:Binding&gt;`标签提供了一种更为灵活的数据绑定方式,它可以定义更复杂的绑定逻辑,包括双向绑定等高级功能。 **使用方法** 假设有一个名为`sourceVar`的变量和一个名为`targetLabel`的Label组件,可以使用`...

    FLEX数据绑定专题

    例如,通过使用Bindable元标签或Bindable类修饰符,可以实现输入控件(如TextInput)与数据模型间的双向绑定: ```mxml &lt;mx:TextInput id="inputField" text="{data.name}" bindable="true" /&gt; ``` 在Flex中,数据...

    flex数据绑定 pdf

    1. **基本数据绑定**:讲解如何设置单向和双向数据绑定,以及它们在不同场景下的应用。 2. **表达式和转换器**:可能详细阐述了如何在绑定表达式中使用函数和运算符,以及如何通过转换器自定义数据的显示方式。 3....

    Flex Bind数据绑定实例代码

    Flex Bind数据绑定是Adobe Flex应用程序开发中的核心特性,它允许开发者在用户...在实际项目中,根据需求选择合适的绑定方式,如单向绑定、双向绑定,结合转换函数和事件处理,能有效提升应用的用户体验和开发效率。

    FLEX 数据绑定专题一(转)

    2. 双向数据绑定:双向数据绑定允许数据在模型和视图之间自由流动。当视图中的组件(如文本框)的值改变时,模型也会同步更新,反之亦然。 数据绑定的工作原理: 在FLEX中,数据绑定使用Observer模式实现。每个绑定...

    flex 数据绑定 dataGrid.rar

    3. **双向绑定**:双向数据绑定允许数据在源对象和目标对象之间流动。常见于输入控件,如文本框,当用户输入时,数据会自动更新到源对象。 4. **表达式语法**:`&lt;s:property attribute="{sourceProperty}" /&gt;`,...

    flex企业应用开发笔记-数据绑定

    Flex提供了`&lt;mx:Form&gt;`组件和`&lt;mx:FormItem&gt;`的`data`属性支持双向绑定。 3. **事件驱动的数据绑定**:在某些情况下,数据变化可能不是立即触发的,而是基于特定事件,如按钮点击。这时,可以使用`&lt;mx:Binding&gt;`...

    flex数据绑定

    例如,`&lt;s:TextInput bindable="true" text="{myData}" /&gt;`是单向绑定,而`&lt;s:FormElement label="Name" id="nameField" data="{myData}" /&gt;`是双向绑定,因为`FormElement`会自动处理用户输入的更新。 4. 表达式与...

    Flex 数据绑定

    在Flex中,数据绑定是一种声明性编程方式,通过简单的语法就能实现双向绑定,即模型变化时更新视图,用户交互改变视图时更新模型。 2. 声明式数据绑定 在Flex中,数据绑定通常使用`=`, `:`或`{}`符号来表示。例如,...

    Flex初级数据绑定代码

    ### Flex初级数据绑定代码解析 #### 一、概述 本文将详细介绍一个Flex应用程序中的...未来在更复杂的项目中,可以进一步探索更多高级的数据绑定技巧,如双向绑定、条件绑定等,以提高应用程序的灵活性和可维护性。

    基于Blazeds的flex和java双向通信

    【基于Blazeds的flex和java双向通信】 在IT领域,构建富互联网应用程序(RIA)时,Flex作为前端用户界面框架,与后端Java服务进行交互是常见的需求。Blazeds是Adobe提供的一种解决方案,它使得Flex和Java之间能够...

    flex例子.flex源程序

    6. **数据绑定**: Flex支持双向数据绑定,使得视图和模型之间的同步变得简单,减少手动更新的工作量。 7. **事件处理**: 通过监听和处理事件,你可以响应用户的交互,实现动态和响应式的应用程序。 8. **Services ...

    flex4-binding-custom-Class.rar_flex_flex4

    使用`&lt;s:BidirectionalBinding&gt;`或`&lt;mx:TwoWayBinding&gt;`可以实现双向绑定,但在这里,我们使用了简化的语法`{双向绑定(...)}`。 5. **高级数据绑定使用**: 数据绑定还可以结合运算符和函数,实现更复杂的逻辑。...

    flex中文帮助文档

    文档会解释如何设置数据绑定,以及双向数据绑定的概念。 6. **Services和Remoting**:Flex提供了与服务器通信的机制,如HTTPService、WebService和AMF。文档会讲解如何调用远程服务,处理响应数据和错误。 7. **...

    flex数据交互_方式

    Flex提供了两种类型的数据绑定:单向绑定和双向绑定。单向绑定意味着数据只能从模型流向视图,而双向绑定则允许数据在模型和视图之间双向流动,使得数据更新更加灵活和实时。 例如,在一个简单的Flex应用程序中,一...

    记事万年历flex源码

    4. **数据绑定**:Flex支持双向数据绑定,使得视图和模型之间的数据更新能够自动同步,简化了代码编写。 5. **事件驱动编程**:用户与界面的交互通常由事件触发,Flex提供了完善的事件处理机制,使得响应用户操作变...

    Flex 页面实例(java)

    9. **数据绑定**:Flex支持双向数据绑定,这意味着UI组件的值可以直接与后台模型关联,当模型数据变化时,界面会自动更新,反之亦然。 10. **Flex应用部署**:Flex应用可以通过SWF文件形式嵌入到HTML页面中,也可以...

Global site tag (gtag.js) - Google Analytics