Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. public class AdminDataLoadingService extends Actor implements IAdminDataLoadingService {
  2.  
  3. [Inject]
  4. public var loadingURLVarsFactory:ILoadingURLVarsFactory;
  5.  
  6. [Inject(name='loadingScriptPath')]
  7. public var loadingScriptPath:String;
  8.  
  9. [Inject]
  10. public var adminXMLToModelProcessor:IAdminXMLToModelProcessor;
  11.  
  12.  
  13. protected var _loadingDataVOType:Class;
  14.  
  15. //--------------------------------------------------------------------------
  16. //
  17. // Initialization
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. public function AdminDataLoadingService()
  22. {
  23. super();
  24. }
  25.  
  26. //--------------------------------------------------------------------------
  27. //
  28. // API
  29. //
  30. //--------------------------------------------------------------------------
  31.  
  32. //---------------------------------------
  33. // IAdminDataLoadingService Implementation
  34. //---------------------------------------
  35.  
  36. public function loadData():void
  37. {
  38. var urlLoader:URLLoader = new URLLoader();
  39. var urlRequest:URLRequest = createLoadRequestForDataType(_loadingDataVOType)
  40. addCommonEventListeners(urlLoader);
  41. urlLoader.load(urlRequest);
  42. }
  43.  
  44. protected function createLoadRequestForDataType(dataClass:Class):URLRequest
  45. {
  46. var urlReq:URLRequest = new URLRequest();
  47. urlReq.url = loadingScriptPath;
  48. urlReq.data = loadingURLVarsFactory.createLoadingURLVarsForDataType(dataClass);
  49. urlReq.method = URLRequestMethod.POST;
  50. return urlReq;
  51. }
  52.  
  53. protected function addCommonEventListeners(urlLoader:URLLoader):void
  54. {
  55. urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  56. urlLoader.addEventListener(Event.COMPLETE, completeHandler);
  57. }
  58.  
  59. protected function removeCommonEventListeners(urlLoader:URLLoader):void
  60. {
  61. urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  62. urlLoader.removeEventListener(Event.COMPLETE, completeHandler);
  63. }
  64.  
  65. protected function ioErrorHandler(e:IOErrorEvent):void
  66. {
  67. //trace("AdminDataLoadingService::ioErrorHandler()");
  68. removeCommonEventListeners(e.target as URLLoader);
  69.  
  70. var evt:AdminDataServiceUpdateEvent = new AdminDataServiceUpdateEvent(AdminDataServiceUpdateEvent.DATA_LOADING_ERROR);
  71. dispatch(evt);
  72. }
  73.  
  74. protected function completeHandler(e:Event):void
  75. {
  76. var urlLoader:URLLoader = e.target as URLLoader;
  77. //trace("urlLoader.data: " + urlLoader.data);
  78. var urlVars:URLVariables = new URLVariables(urlLoader.data);
  79. processLoadedData(urlVars);
  80. dispatchServiceCompleteEvent();
  81. }
  82.  
  83. protected function processLoadedData(urlVars:URLVariables):void
  84. {
  85. trace("AdminDataLoadingService::processLoadedData() should be overwritten in the extending class.");
  86. }
  87.  
  88. private function dispatchServiceCompleteEvent():void
  89. {
  90. var evt:AdminDataServiceUpdateEvent = new AdminDataServiceUpdateEvent(AdminDataServiceUpdateEvent.DATA_LOADING_COMPLETED);
  91. dispatch(evt);
  92. }
  93.  
  94. }
Add Comment
Please, Sign In to add comment