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標籤:

   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"?>

發佈了46 篇原創文章 · 獲贊 3 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章