2011年6月12日

Flex Training ex1_04 HTTPService 動態資料綁定

在練習三的時候我們的資料來源是使用當下的Input來決定,但是有時候資料的來源可能是動態的在更動,所以在練習四主要是使用動態綁定的方式來做為資料來源:
資料來源為網路上的XML檔案,我們使用DropList裡面的dataProvider屬性為其綁定。

步驟一 : 在Declarations中宣告我們的HttpService並且指定服務的id以及服務位址

    <fx:Declarations>
        <s:HTTPService
id="employeeService" url="http://adobetes.com/f45iaw100/remoteData/employees.xml"
/>
    </fx:Declarations>


步驟二 : 使用該服務的send()方法得到HTTP響應,注意必須在creationComplete

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955" minHeight="850"
               creationComplete="employeeService.send()"
               >


步驟三 : 綁定資料到DropList,注意,因為回傳資料型態有名稱、電話等等..所以要指定它的欄位為lastName。

<s:DropDownList
id="dropDownList" dataProvider="{employeeService.lastResult.employees.employee}"
labelField="lastName"
/>


完成!!  運行瀏覽器看看結果吧

關於這篇所用到的類別額外學習


<s:HTTPService>
You use the <mx:HTTPService> tag to represent an HTTPService object in an MXML file. When you call the HTTPService object's send() method, it makes an HTTP request to the specified URL, and an HTTP response is returned. Optionally, you can pass parameters to the specified URL. When you do not go through the server-based proxy service, you can use only HTTP GET or POST methods. However, when you set the useProxy property to true and you use the server-based proxy service, you can also use the HTTP HEAD, OPTIONS, TRACE, and DELETE methods.


mx.core.UIComponent.creationComplete
Dispatched when the component has finished its construction, property processing, measuring, layout, and drawing. 
(意思就是當這個元件完成了它初始化的工作,並且可以顯示到畫面上)

DropDownListBase.dataProvider(value:IList):void
The data provider for this DataGroup. It must be an IList.
There are several IList implementations included in the Flex framework, including ArrayCollection, ArrayList, and XMLListCollection.
This property can be used as the source for data binding.


DropDownListBase.labelField(value:String):void
The name of the field in the data provider items to display as the label. If labelField is set to an empty string (""), no field will be considered on the data provider to represent label. The labelFunction property overrides this property.

沒有留言:

ShareThis