I am learning the networking portion of Flex 4.5 right now, and the course I am reviewing recommends that I use the <s:CallResponder>
class whenever I make a call to a server, either with the <s:HTTPService>
component or with my own custom service call to a database.
Both in the course itself and in the Adobe documentation, I cannot find a really good description as to why I should take the approach. Could someone please describe why this is a good idea, and perhaps provide a case where it is highly recommended?
See this example below for a simple case where I am using it. I declare an instance of the class inside of the <fx:Declarations>
tagset, and I use it inside of the fetchData()
method:
<?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>