【永恒的瞬間】
          ?Give me hapy ?
          找到了一個Flex?RemoteObject結合CFC使用的例子.在這里我來詳細講一下.
          主要三個文件.
          Flex:?main.mxml
          AS:?Test.as
          CFC:?test.cfc


          先教大家配置一下.
          main.mxml放到Flex的根目錄下.
          Test.as放到Flex的根目錄下的cfcRemoting文件內.
          test.cfc放到Coldfusion的根目錄下的cfcRemoting文件內.


          main.mxml文件的內容:
          程序代碼:
          <?xml?version="1.0"?encoding="utf-8"?>
          <mx:Application?xmlns:mx="http://www.macromedia.com/2003/mxml"?>
          ?<mx:Script>
          ?<![CDATA[
          ??var?dependency:cfcRemoting.Test;
          ?]]>
          ?</mx:Script>
          ?<mx:RemoteObject?
          ??endpoint="http://localhost:8300/flashservices/gateway"
          ??source="cfcRemoting.test"
          ??id="ro"
          ?/>
          ?<mx:DateFormatter?formatString="hh:mm:ss"?id="dateFormatter"?/>
          ?<mx:Panel?title="CFC?Remoting"?width="350">
          ??<mx:List?id="objList"?dataProvider="{ro.getObjectArray.result}"?width="100%"?/>
          ??<mx:TextInput?editable="false"?text="{objList.selectedItem}"?width="100%"?/>
          ??<mx:ControlBar>
          ???<mx:Label?text="Number?of?objects:"/>
          ???<mx:NumericStepper?id="amount"?minimum="1"?maximum="500"?value="/5"?/>
          ???<mx:Button?label="invoke"?click="ro.getObjectArray(amount.value)"?/>
          ??</mx:ControlBar>
          ?</mx:Panel>
          </mx:Application>

          1.首先分析看一下他的外觀是如何的.

          外觀用了一個Panel:用于裝下面的組件.一個List:用于存放從CFC傳回來的.一個TextInput:用于顯示List被選的條目.一個:ControlBar:用于裝Label,?NumericStepper,與Button.?NumericStepper用于用戶所需生成的對象數,Button用于調用CFC.
          程序代碼:
          <?xml?version="1.0"?encoding="utf-8"?>
          <mx:Application?xmlns:mx="http://www.macromedia.com/2003/mxml"?>
          ?<mx:Panel?title="CFC?Remoting"?width="350">
          ??<mx:List?id="objList"?width="100%"/>
          ??<mx:TextInput?editable="false"?width="100%"?/>
          ??<mx:ControlBar>
          ???<mx:Label?text="Number?of?objects:"/>
          ???<mx:NumericStepper?id="amount"?minimum="1"?maximum="500"?value="5"?/>
          ???<mx:Button?label="invoke"?/>
          ??</mx:ControlBar>
          ?</mx:Panel>
          </mx:Application>
          ?

          好了,外觀出來了.
          2.再看一下除外觀標簽外的標簽.
          程序代碼:
          <mx:Script>
          ?<![CDATA[
          ??//這里聲明了一個CFC組件的一個對象.
          ??var?dependency:cfcRemoting.Test;
          ?]]>
          ?</mx:Script>
          ?<mx:RemoteObject?
          ??//?endpoint屬性指定網關,這里根大家的配置所填.我這里的是CF7.0的.如果用CFMX的端口改回8500
          ??endpoint=http://localhost:8300/flashservices/gateway
          ??//?source屬性指定CFC的文件
          ??source="cfcRemoting.test"
          ??//id屬性指定實例名,用于在程序中的引用
          ??id="ro"
          ?/>
          ?//formatString屬性指定時間的格式
          ?//id屬性指定實例名,用于在程序中的引用
          ?<mx:DateFormatter?formatString="hh:mm:ss"?id="dateFormatter"?/>
          ?
          3.再看看外觀的代碼:
          程序代碼:
          //標題設為"CFC?Remoting"
          //寬設為:?350
          <mx:Panel?title="CFC?Remoting"?width="350">
          ??//?dataProvider屬性等于ro.?getObjectArray.result得回來的.
          ??<mx:List?id="objList"?dataProvider="{ro.getObjectArray.result}"?width="100%"?/>
          ??//?text屬性是等于"objList"中被選擇的條目
          ??<mx:TextInput?editable="false"?text="{objList.selectedItem}"?width="100%"?/>
          ??<mx:ControlBar>
          ???<mx:Label?text="Number?of?objects:"/>
          ???<mx:NumericStepper?id="amount"?minimum="1"?maximum="500"?value="/5"?/>
          ???//?click事件調用ro.getObjectArray(),將NumericStepper的傳到CFC里
          ???<mx:Button?label="invoke"?click="ro.getObjectArray(amount.value)"?/>
          ??</mx:ControlBar>
          ?</mx:Panel>

          4.test.cfc的內容
          程序代碼:
          <cfcomponent>
          ?<cfset?this.remoteClass="cfcRemoting.Test">
          ?<cffunction?name="getObject"?access="remote"?returntype="struct">
          ??<cfargument?name="num"?type="numeric"?required="yes">
          ??<cfset?var?obj=StructNew()>
          ??<cfset?obj["_remoteClass"]?=?this.remoteClass>
          ??<cfset?obj["date"]?=?now()>??
          ??<cfset?obj["name"]?=?"Object?#num#">??
          ??<cfreturn?obj>
          ?</cffunction>
          ?<cffunction?name="getObjectArray"?access="remote"?returntype="array">
          ??<cfargument?name="amount"?type="numeric"?required="no"?default="1">
          ??<cfset?arr?=?ArrayNew(1)>
          ??<cfloop?from="1"?to=#amount#?index="j">
          ???<cfset?arr[j]=#getObject(j)#>
          ??</cfloop>
          ??<cfreturn?arr>
          ?</cffunction>
          </cfcomponent>

          這個組件有兩個方法.?getObject()getObjectArray().
          這里的getObject()方法是由getObjectArray()方法去調用的.
          getObjectArray()方法是main.mxml中的ro對象所調用的,
          看一下他們做了些什么
          getObjectArray()方法就根據main.mxml中的NumericStepper來動態生成數組個數.
          每個數組都調用getObject()?方法來填充數據為一個結構.
          getObject()?方法就將每個數組都賦上一個結構。
          結構體里有:["_remoteClass"],["date"],["name"]三條。
          5.Test.as的代碼:
          程序代碼:
          class?cfcRemoting.Test?{
          ?//定義一個name
          ?public?var?name:String;
          ?//定義一個時間
          ?public?var?date:Date;
          ?//構造函數
          ?public?function?Test()?{
          ?}
          ?public?function?get?label():String?{
          ??//返回這個到main.mxml的List里
          ??return?name?+?"?(label?test)";
          ?}
          ?
          ?public?function?toString():String?{
          ??//返回這個到main.mxml的TextInput里
          ??return?"Hi,?this?is?"?+?name?+?",?created?"?+?date.getHours()?+?":"?+?date.getMinutes()?+?":"?+?date.getSeconds()?+?":"?+?date.getMilliseconds();
          ?}
          }


          好了.完成了.大家試用一下吧.
          posted on 2007-03-02 14:10 ???MengChuChen 閱讀(852) 評論(0)  編輯  收藏 所屬分類: flex2.0
          主站蜘蛛池模板: 鲁山县| 潮安县| 合阳县| 兰西县| 西乌珠穆沁旗| 韩城市| 邹城市| 寿宁县| 始兴县| 密云县| 绩溪县| 金堂县| 北票市| 商河县| 石嘴山市| 浑源县| 永德县| 项城市| 望城县| 汾阳市| 昭苏县| 通江县| 五河县| 额敏县| 玛多县| 武穴市| 育儿| 万宁市| 合肥市| 教育| 雅江县| 家居| 庆安县| 高青县| 凤冈县| 临潭县| 沧源| 靖西县| 永兴县| 罗江县| 保康县|