views:

934

answers:

3

I am trying to use the following code to load styles from an external SWF, but I keep getting an ActionScript error when the URL is invalid:

Error: Unable to load style(Error #2036: Load Never Completed. URL: http://localhost/css/styles.swf): ../css/styles.swf.
at <anonymous>()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManagerImpl.as:858] 


private function loadStyles():void
{
    try
    {
        var styleEvent:IEventDispatcher = 
            StyleManager.loadStyleDeclarations("../styles.swf");

        styleEvent.addEventListener(StyleEvent.COMPLETE, 
                                                    loadStyle_completeHandler);

        styleEvent.addEventListener(StyleEvent.ERROR, 
                                                    loadStyle_errorHandler);
    }
    catch (error:Error)
    {
        useDefault();
    }
}

private function loadStyle_completeHandler(event:StyleEvent):void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_completeHandler);

    goToNextStep();
}

private function loadStyle_errorHandler(event:StyleEvent):void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_errorHandler);

    useDefault();
}

I basically want to go ahead an use the default styles w/o the user seeing the error if this file can't be loaded - but I can't seem to find any way to do this.

+1  A: 

Interesting problem. Try removing the removeEventListener call, or commenting it out; in my brief tests it appeared the event handler was being called twice (I'm not immediately sure why, although I suspect it has to do with style inheritance), and commenting that line did the trick.

If you have the same result, you might try just checking for the listener (using hasEventListener) first, before attaching it in your loadStyles() function, instead. Hope it helps!

Christian Nunciato
Thanks, I'll look into that.
Eric Belair
A: 

** Not an answer, but an update:

FYI, this is the ActionScript code in the Source of mx.styles.StyleManagerImpl that is run when you call StyleManager.loadStyleDeclarations(). I ran the debugger and added a breakpoint at the Line 858("throw new Error(errorText);"), and the breakpoint was caught. I'm thinking it shouldn't be caught there, but the previous IF ("if (styleEventDispatcher.willTrigger(StyleEvent.ERROR))") should be run instead.

public function loadStyleDeclarations2(
     url:String, update:Boolean = true,
     applicationDomain:ApplicationDomain = null,
     securityDomain:SecurityDomain = null):
     IEventDispatcher
{
    var module:IModuleInfo = ModuleManager.getModule(url);

    var readyHandler:Function = function(moduleEvent:ModuleEvent):void
    {
        var styleModule:IStyleModule =
            IStyleModule(moduleEvent.module.factory.create());

        styleModules[moduleEvent.module.url].styleModule = styleModule;

        if (update)
            styleDeclarationsChanged();
    };

    module.addEventListener(ModuleEvent.READY, readyHandler,
                            false, 0, true);

    var styleEventDispatcher:StyleEventDispatcher =
                                    new StyleEventDispatcher(module);

    var errorHandler:Function = function(moduleEvent:ModuleEvent):void
    {
  var errorText:String = resourceManager.getString(
   "styles", "unableToLoad", [ moduleEvent.errorText, url ]);

        if (styleEventDispatcher.willTrigger(StyleEvent.ERROR))
        {
            var styleEvent:StyleEvent = new StyleEvent(
                StyleEvent.ERROR, moduleEvent.bubbles, moduleEvent.cancelable);
            styleEvent.bytesLoaded = 0;
            styleEvent.bytesTotal = 0;
            styleEvent.errorText = errorText;
            styleEventDispatcher.dispatchEvent(styleEvent);
        }
        else
        {
            throw new Error(errorText);
        }
    };

    module.addEventListener(ModuleEvent.ERROR, errorHandler,
                            false, 0, true);

    styleModules[url] =
        new StyleModuleInfo(module, readyHandler, errorHandler);

    // This Timer gives the loadStyleDeclarations() caller a chance
    // to add event listeners to the return value, before the module
    // is loaded.
    var timer:Timer = new Timer(0);
    var timerHandler:Function = function(event:TimerEvent):void
    {
        timer.removeEventListener(TimerEvent.TIMER, timerHandler);
        timer.stop();
        module.load(applicationDomain, securityDomain);
    };

    timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);

    timer.start();

    return styleEventDispatcher;
}
Eric Belair
A: 

I debugged the source, and found that the ERROR Event is being triggered twice. So, I simply set a flag the first time the ERROR event handler is triggered, and check that flag for a value of true before continuing:

private var isErrorTriggered:Boolean; // Default is false

private function loadStyle_completeHandler(event:StyleEvent):void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_completeHandler);

    goToNextStep();
}

private function loadStyle_errorHandler(event:StyleEvent):void
{
    if (isErrorTriggered)
        return;

    isErrorTriggered = true;

    useDefault();
}
Eric Belair