2011年6月27日

E4X的基本使用方法

在ActionScript3.0中,以ECMAScript for XML(E4X)形式提供了原生性的XML支援功能。本篇將會介紹E4X的強大搜尋功能,首先我們必須了解XML與XMLList的差別。

XML
XMLList
<catalog>
    <category name="vegetables1">
</category>
    <category name="vegetables2">
</category>
</catalog>
<category name="vegetables1">
</category>
<category name="vegetables2">
</category>


首先查看上圖,XML具有很強的規則性,每個XML格式必須只能有一個根節點(root),但在XMLList中就沒有這個規定,它可以容許多個根節點,因此我們可以說這個XMLList的長度(length)為2。

為什麼要介紹這個呢!? 那是因為在E4X中我們所要操控的就是XMLList,當然XML格式也是一種XMLList的CASE。



首先假設我們目標xml文件如下表顯示 :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <category name="vegetables">
        <product name="lettuce"
                 cost="1.95">
            <unit>bag</unit>
            <desc>Cleaned and bagged</desc>
        </product>
        <product name="carrots"
                 cost="2.95">
            <unit>pound</unit>
            <desc>Baby carrots, cleaned and peeled</desc>
        </product>
    </category>
    <category name="fruit">
        <product name="apples"
                 cost="1.95">
            <unit>each</unit>
            <desc>Sweet Fuji</desc>
        </product>
        <berries>
            <product name="raspberries"
                     cost="3.95">
                <unit>pint</unit>
                <desc>Firm and fresh</desc>
            </product>
            <product name="strawberries"
                     cost="2.95">
                <unit>pint</unit>
                <desc>Deep red and juicy</desc>
            </product>
        </berries>
    </category>
</catalog>


在E4X中我們使用點運算子(.)來操作,這點與我們熟悉的(物件.屬性)操作方式雷同。
以下是一些E4X使用的例子。


一些簡單的查詢表達式 :
表達式
結果
category.product
<product name="lettuce"
         cost="1.95">
    <unit>bag</unit>
    <desc>Cleaned and bagged</desc>
</product>
<product name="carrots"
         cost="2.95">
    <unit>pound</unit>
    <desc>Baby carrots, cleaned and peeled</desc>
</product>
<product name="apples"
         cost="1.95">
    <unit>each</unit>
    <desc>Sweet Fuji</desc>
</product>
category.berries.product
<product name="raspberries"
         cost="3.95">
    <unit>pint</unit>
    <desc>Firm and fresh</desc>
</product>
<product name="strawberries"
         cost="2.95">
    <unit>pint</unit>
    <desc>Deep red and juicy</desc>
</product>


比較複雜的查詢 :
條件搜尋,其中@表示搜尋的目標為屬性
category.berries.product.(@cost=="3.95")
<product name="raspberries"
         cost="3.95">
    <unit>pint</unit>
    <desc>Firm and fresh</desc>
</product>


條件搜尋,對子節點進行比對搜尋
category.product.(unit=="bag")
<product name="lettuce" cost="1.95">
  <unit>bag</unit>
  <desc>Cleaned and bagged</desc>
</product>


介紹的這些方法已經可以用在很多的場合囉!!
如果有更好的知識,請觀看的人不吝賜教 :D

2011年6月26日

HTTPService及FaultEvent

因為HTTPService是不可視的,因此我們必須將它放在<fx:Declarations>中
HTTPService並不會自己發出POST或是GET請求,必須由使用者來呼叫send()方法啟用,在HTTPService屬性中必須明確告知URL,這個URL可以是處於同一目錄下的靜態xml檔案,也可以是遠程的資源(JSP,ASP等動態產生文件)。

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


如果給HTTPService錯誤的URL此時會產生錯誤,並且會有FaultEvent產生,如果我們沒有為HTTPService實現錯誤產生的Handler錯誤會在瀏覽器中顯示,但通常我們是需要印出這些內容來做除錯之功用。

故意產生錯誤
<fx:Declarations>
    <s:HTTPService id="employeeService"
                   url="http://adobetes.com/f45iaw100/remoteData/employees.xm"
                   result="employeeService_resultHandler(event)"
                   fault="faultHandler(event)"/>
</fx:Declarations>

當錯誤發生時handler就會捕捉到FaultEvent事件
protected function faultHandler(event:FaultEvent):void
{
    Alert.show(event.fault.faultString,"Error Information");
}

錯誤訊息

Flex中使用對話框

對話框是常常必須使用的,不管是在使用提示訊息還是功能選擇,他都扮演一個很重要的角色,在Flex中使用對話框的方式非常簡單,只需要透過Alert類別中的靜態方法show就可以顯示了(與Android Tosat很類似),以下是使用的方式。

Show方法
public static function show(text:String = "", title:String = "", flags:uint = 0x4, parent:Sprite = null, closeHandler:Function = null, iconClass:Class = null, defaultButtonFlag:uint = 0x4,

彈出 Alert 控制項的靜態方法。在 Alert 控制項中選擇一個按鈕或按下 Esc 鍵時,將關閉該控制項。

參數
text:String (default = "") — 在 Alert 控制項中顯示的文本字串。此文本將在警告對話方塊中居中顯示。

title:String (default = "") — 標題列中顯示的文本字串。此文本左對齊。

flags:uint (default = 0x4) — Alert 控制項中放置的按鈕。有效值為 Alert.OK、Alert.CANCEL、Alert.YES 和 Alert.NO。預設值為 Alert.OK。使用按位元 OR 運算子可顯示多個按鈕。例如,傳遞 (Alert.YES | Alert.NO) 顯示“是”和“否”按鈕。無論按怎樣的順序指定按鈕,它們始終按照以下順序從左到右顯示:“確定”、“是”、“否”、“取消”。

parent:Sprite (default = null) — Alert 控制項在其上居中的對象。

closeHandler:Function (default = null) — 按下 Alert 控制項上的任意按鈕時將調用的事件處理函數。傳遞給此處理函數的事件物件是 CloseEvent 的一個實例;此物件的 detail 屬性包含 Alert.OK、Alert.CANCEL、Alert.YES 




以三個小例子解釋上面的參數
// 最基本的提示訊息格式
protected function button1_clickHandler(event:MouseEvent):void
{
    Alert.show("你點擊了按鈕一","提示訊息");
}
           
// 帶有內建YES,NO選項的對話框
protected function button2_clickHandler(event:MouseEvent):void
{
    Alert.show("你點擊了按鈕二,是否要關閉","提示訊息
    ",Alert.YES|Alert.NO,btn3,closeHandler);
}
           
// 自定義對話框按鈕的對話框
protected function button3_clickHandler(event:MouseEvent):void
{
    Alert.yesLabel = "是";
    Alert.noLabel  = "不";
    Alert.show("你點擊了按鈕三,是否要關閉","提示訊息
     ",Alert.YES|Alert.NO,btn2,closeHandler2);
}

在button1_clickHandler中我們會顯示出一個最簡單的提示框,會顯示出我們要說明的文字以及標題只有一個按鈕可以按。



在按鈕二中我們可以看到我們新增了三個參數在show中,分別為flag,parent,closeHandler其中



flag
表示我們對話框裏頭的按鈕,它對應了Alert中定義的一些整數,假如我們要顯示OK就對應到Alert.OK這個變數中,這裡我們可以使用 | 將多個需求OR起來。
parent
表示對話框要顯示的位置,這裡我們設定顯示位置為btn3,因此它就會以btn3的座標作為對話框顯示左上點的座標。
closeHandler
在裡面我們可以定義我們點擊對話框所要處理的事件,我們在點擊按鈕時會產生AlertEvent事件,因此我們的Handler必須依照這個事件類型來做不同的處理。


closeHandler的例子

private function closeHandler2(e:CloseEvent):void
{
    if(e.detail == Alert.YES){
        Alert.show("你點擊了是","訊息");
    }
    else{
        Alert.show("你點擊了否","訊息");
    }
}
































TextInput 密碼顯示輸入

有時候我們必須要使用TextInput來打密碼,但又不想顯示出來
更改方法很簡單,只需要將TextInput中的displayAsPassword設置為true即可。


<s:TextInput displayAsPassword="true"/>

ShareThis