4 Replies Latest reply on Mar 9, 2012 2:51 AM by IronDude84

    Duplicate ResourceEvent.ERROR with ResourceManager

    jbfiedler2

      Hello, I am working with resource modules and I believe I am doing everything by the book. However, when setting up error handling I ran into an interesting issue where I get two ResourceEvent.ERROR events when I try to load a nonexistant resource module.

       

      Running the following code I get output of:

      [ResourceManagerService.changeLocale]
      [ResourceManagerService.addResourceEventHandlers]
      [ResourceManagerService.onResourceModuleErrorHandler]
      [ResourceManagerService.onResourceModuleErrorHandler]

       

      If I remove the event handlers after the first ResourceEvent.ERROR event I get a runtime error "Error: Unable to load resource module from locale/es_ES_ResourceModule.swf " .

       

      Is there something wrong with this code or possibly ResourceManagerImpl.as?  I am using FlashBuilder 4 and Flex SDK 4.1.0 build 16076. Thanks in advance for your help.

       

      <?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"
                     creationComplete="application1_creationCompleteHandler(event)"
                     minWidth="955" minHeight="600">
          <fx:Script>
              <![CDATA[
                  import mx.events.FlexEvent;
                  import mx.events.ResourceEvent;
                  import mx.resources.IResourceManager;
                  import mx.resources.ResourceManager;
                  public var myResourceManager:IResourceManager;
                 
                  private var _resourceManagerEventDispatcher:IEventDispatcher;
                 
                  protected function application1_creationCompleteHandler(event:FlexEvent):void {
                      myResourceManager = ResourceManager.getInstance();
                      changeLocale('es_ES');
                  }
                 
                  public function changeLocale($newLocale:String):void {
                      trace("[ResourceManagerService.changeLocale]");
                      var hasLocale:Boolean = (resourceManager.getLocales().indexOf($newLocale) != -1);
                     
                      if(hasLocale) {   
                          onResourceModuleCompleteHandler(null)
                      } else {
                          var resourceModuleURL:String = getResourceModuleUrl($newLocale);
                         
                          try {
                              _resourceManagerEventDispatcher = resourceManager.loadResourceModule(resourceModuleURL);
                              addResourceEventHandlers();
                          } catch (err:Error) {
                              onResourceModuleErrorHandler(null);
                          }
                      }
                  }
                 
                  public function getResourceModuleUrl($locale:String):String {
                      return "locale/" + $locale + "_ResourceModule.swf";
                  }
                 
                  private function addResourceEventHandlers():void {
                      trace("[ResourceManagerService.addResourceEventHandlers]");
                      if(_resourceManagerEventDispatcher) {
                          _resourceManagerEventDispatcher.addEventListener(ResourceEvent.ERROR, onResourceModuleErrorHandler);
                          _resourceManagerEventDispatcher.addEventListener(ResourceEvent.COMPLETE, onResourceModuleCompleteHandler);
                      }
                  }
                 
                  private function removeResourceEventHandlers():void {
                      trace("[ResourceManagerService.removeResourceEventHandlers]");
                      if(_resourceManagerEventDispatcher) {
                          _resourceManagerEventDispatcher.removeEventListener(ResourceEvent.ERROR, onResourceModuleErrorHandler);
                          _resourceManagerEventDispatcher.removeEventListener(ResourceEvent.COMPLETE, onResourceModuleCompleteHandler);
                      }
                  }
                 
                  private function onResourceModuleCompleteHandler(event:ResourceEvent):void {
                      trace("[ResourceManagerService.onResourceModuleCompleteHandler]");
                  }
                 
                  private function onResourceModuleErrorHandler(event:ResourceEvent):void {
                      //removeResourceEventHandlers();
                      trace("[ResourceManagerService.onResourceModuleErrorHandler]");
                  }
              ]]>
          </fx:Script>
      </s:Application>