Skip to content

Commit

Permalink
adding an operation that wraps URLLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Mar 10, 2011
1 parent 4210785 commit c45f9c4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/operations/ServiceOperation.as
Expand Up @@ -8,7 +8,7 @@ package operations

/**
* An asynchronous operation that wraps the execution of a Flex RPC service. This operation
* can be used with either <code>HTTPService</code>s or <code>RemoteObject</code>s.
* can be used with either <code>HTTPMultiService</code>s or <code>RemoteObject</code>s.
*
* @author Dan Schultz
*/
Expand Down Expand Up @@ -51,7 +51,7 @@ package operations

private function handleAsyncTokenFault(event:FaultEvent, token:AsyncToken):void
{
fault(event.fault.faultString, event.fault.faultDetail);
fault(event.fault.faultString, event.fault.faultDetail, event.fault.faultCode);
}
}
}
81 changes: 81 additions & 0 deletions src/operations/URLLoaderOperation.as
@@ -0,0 +1,81 @@
package operations
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;

/**
* An asynchronous operation that wraps Flash's <code>URLLoader</code> to perform
* a network operation.
*
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html URLLoader
* @author Dan Schultz
*/
public class URLLoaderOperation extends NetworkOperation
{
private var _request:URLRequest;
private var _loader:URLLoader;

/**
* Constructor.
*
* @param request The request to execute.
* @param dataFormat The data format of the result. Use one of the constants defined on
* <code>URLLoaderDataFormat</code>.
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoaderDataFormat.html URLLoaderDataFormat
*/
public function URLLoaderOperation(request:URLRequest, dataFormat:String = URLLoaderDataFormat.TEXT)
{
super();

_request = request;
_loader = new URLLoader();
_loader.dataFormat = dataFormat;
}

/**
* @inheritDoc
*/
override protected function request():void
{
super.request();

_loader.addEventListener(IOErrorEvent.IO_ERROR, handleLoaderIOError);
_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleLoaderSecurityError);
_loader.addEventListener(Event.COMPLETE, handleLoaderComplete);
_loader.load(_request);
}

/**
* @inheritDoc
*/
override protected function cancelRequest():void
{
super.cancelRequest();

try {
_loader.close();
} catch (e:Error) {

}
}

private function handleLoaderIOError(event:IOErrorEvent):void
{
fault(event.text, event.text);
}

private function handleLoaderSecurityError(event:SecurityErrorEvent):void
{
fault(event.text, event.text);
}

private function handleLoaderComplete(event:Event):void
{
result(_loader.data);
}
}
}

0 comments on commit c45f9c4

Please sign in to comment.