【发布时间】:2012-02-15 03:50:13
【问题描述】:
我现在正在学习 Flex 4.5 的网络部分,我正在复习的课程建议我在调用服务器时使用 <s:CallResponder> 类,无论是使用 <s:HTTPService> 组件还是我自己的组件对数据库的自定义服务调用。
无论是在课程本身还是在 Adobe 文档中,我都找不到一个很好的描述来说明为什么我应该采用这种方法。有人可以描述一下为什么这是一个好主意,或者提供一个强烈推荐的案例吗?
请参阅下面的示例,了解我使用它的简单案例。我在<fx:Declarations> 标签集中声明了一个类的实例,并在fetchData() 方法中使用它:
<?xml version="1.0" encoding="utf-8"?>
<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="600"
skinClass="skins.CustomAppSkin">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var booksCollection:ArrayCollection;
private function formatPrice(data:Object, columns:GridColumn):String {
return priceFormatter.format(data.price);
}
protected function fetchData(event:MouseEvent):void {
booksResponder.token = books.send();
}
protected function processXML(event:ResultEvent):void {
this.booksCollection = event.result.catalog.book;
}
protected function loadHandler(event:FaultEvent):void {
Alert.show(event.fault.faultString, event.fault.faultCode);
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="books" url="data/books.xml"/>
<s:CurrencyFormatter id="priceFormatter" currencySymbol="$" fractionalDigits="2" trailingZeros="true" useCurrencySymbol="true"/>
<s:CallResponder id="booksResponder" result="processXML(event)" fault="loadHandler(event)"/>
</fx:Declarations>
<s:Panel id="panel" title="Products" horizontalCenter="0">
<s:DataGrid dataProvider="{booksCollection}" height="400">
<s:columns>
<s:ArrayList>
<s:GridColumn headerText="Title" width="250" dataField="title"/>
<s:GridColumn headerText="Author" dataField="author"/>
<s:GridColumn headerText="Genre" width="100" dataField="genre"/>
<s:GridColumn headerText="Publish Date" width="100" dataField="publish_date"/>
<s:GridColumn headerText="Description" width="400" dataField="description"/>
<s:GridColumn headerText="Price (USD)" width="100" dataField="price" labelFunction="formatPrice"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:controlBarContent>
<s:Button id="getData" label="Get Data" click="fetchData(event)"/>
</s:controlBarContent>
</s:Panel>
</s:Application>
【问题讨论】:
标签: flash apache-flex flex4 flash-builder httpservice