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>
Maybe some "historical" background would help. The name "Responder" refers to the way
NetConnection
class, which is the Flash player built-in API handles the communication. When you make a call to NetConnection (which is commonly used for either plain HTTP connection, where messages are encoded in AMF, or for video download / streaming, which is usually an RTMP-variant connection). Responder is used with NetConnection to communicate using AMF encoding over HTTP.There is a built-in
Responder
class, which is used with NetConnection to store references to two callbacks which will be called in case of successful communication and in the case of communication failure.The above scheme is different from other networking API that exist in Flash player. It's hard to think about the reason, but it's been like that since AS2. In AS2, however, there was LoadVars object, which also used callbacks for fault and success messages. XML class behaved in a similar way - you would also create two callback functions and assign them to default XML handlers. It's not impossible that responder is a rudiment of the pre-AS3 design.
When Adobe (for the reasons known only to them alone) decided to pile up a bunch of indirection layers on top of NetConnection (known in Flex as
RemoteObject
) they retained the old design choices. They also made more recent API behave in a more archaic way. Thus, for example,HTTPService
, which is basically theURLLoader
in disguise, behaves the same way as RemoteObject. This probably helped somewhat in MXML templates, but as soon as you realize that MXML language is extremely limiting, the relative merits of features tailored specifically for MXML layout will rather become a plaque made up of multiple questionable design decisions.Amy: afaik DCD feature (the code generation for services used in Flash Builder) has not been released to Apache - it's a part of Flash Builder. So, it's unlikely to change, unless Adobe will change that themselves.