找到了一個
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 閱讀(847)
評論(0) 編輯 收藏 所屬分類:
flex2.0