<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>AaronHardy.com &#187; loadStyleDeclarations</title>
	<atom:link href="http://aaronhardy.com/tag/loadstyledeclarations/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronhardy.com</link>
	<description>For all your Aaron Hardy needs.</description>
	<lastBuildDate>Thu, 12 Jan 2012 17:51:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Loading a Remote Module into a Local App</title>
		<link>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/</link>
		<comments>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 02:39:24 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[crossdomain]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[IModuleInfo]]></category>
		<category><![CDATA[loadResourceModule]]></category>
		<category><![CDATA[loadStyleDeclarations]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[ModuleInfo]]></category>
		<category><![CDATA[ModuleManager]]></category>
		<category><![CDATA[ModuleMarshaller]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[StyleManager]]></category>
		<category><![CDATA[SWF is not a loadable module]]></category>
		<category><![CDATA[Unable to load resource module]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=295</guid>
		<description><![CDATA[At work, we deal a lot with loading modules residing on remote servers into our applications running locally.  By locally I mean from a local path on our hard drives (the default Flex Builder run/debug settings), not on a local web server instance (localhost).  Depending on what type of module we are loading, we would [...]]]></description>
			<content:encoded><![CDATA[<p>At work, we deal a lot with loading modules residing on remote servers into our applications running locally.  By locally I mean from a local path on our hard drives (the default Flex Builder run/debug settings), not on a local web server instance (localhost).  Depending on what type of module we are loading, we would normally use one of Flex&#8217;s built-in functions to load modules:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Loading a language resource module:</span>
resourceManager.<span style="color: #006600;">loadResourceModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'http://aaronhardy.com/en_US_ResourceModule.swf'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Loading a style module (e.g., a compiled font swf):</span>
StyleManager.<span style="color: #006600;">loadStyleDeclarations</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://aaronhardy.com/fonts/Astroid.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Loading miscellaneous modules:</span>
<span style="color: #000000; font-weight: bold;">var</span> moduleInfo:IModuleInfo = ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'http://aaronhardy.com/MyModule.swf'</span><span style="color: #66cc66;">&#41;</span>;
moduleInfo.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>But these methods of importing modules result in errors like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">Unable to <span style="color: #0066CC;">load</span> resource module from http:<span style="color: #808080; font-style: italic;">//aaronhardy.com/en_US_ResourceModule.swf</span>
<span style="color: #0066CC;">Error</span>: Unable to <span style="color: #0066CC;">load</span> style<span style="color: #66cc66;">&#40;</span>SWF is <span style="color: #0066CC;">not</span> a loadable module<span style="color: #66cc66;">&#41;</span>: http:<span style="color: #808080; font-style: italic;">//aaronhardy.com/fonts/Astroid.swf.</span></pre></div></div>

<p><span id="more-295"></span>One way to solve these issues is by <a href="http://bradywhite.net/2008/05/05/flex-3-how-to-load-a-complied-css-swf/" target="_blank">using a crossdomain policy file on the server and then using a local apache instance to run the application</a>.  In my talks with the folks at Adobe, this seems to be their standard workflow which may be why we haven&#8217;t heard more about these problems.  However, my workflow is to NOT have a local web <a href="http://www.hostingobserver.com/server-hosting.php" target="_blank">hosting server</a> running and I don&#8217;t want to have one running just to get around these errors.</p>
<p>As of Flex SDK 3.2, Adobe introduced a new parameter to IModuleInfo.load() for raw module bytes.  As a result, we can now retrieve the remote module&#8217;s raw bytes using a URLLoader and then load them directly as a module using the IModuleInfo.load() method.  The module is now part of the same security domain as the main application and no errors are thrown.</p>
<p>I&#8217;ve bundled up this logic into a class called ModuleMarshaller which is found below.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">aaronhardy</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">ErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">SecurityErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoaderDataFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">ModuleEvent</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">modules</span>.<span style="color: #006600;">IModuleInfo</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">modules</span>.<span style="color: #006600;">ModuleManager</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Dispatched once module loading is complete.
	 */</span>
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;ready&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;mx.events.ModuleEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Dispatched if an error is encountered while loading the module.
	 */</span>
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;error&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;mx.events.ModuleEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Loads a remote module.  Specifically, this is a workaround to allow an application being run
	 * locally (not using a web server) to load a module residing on a remote server.
	 * Usually, when attemping to do so using resourceManager.loadResourceModule(),
	 * StyleManager.loadStyleDeclarations(), or IModuleInfo.load() without passing in bytes
	 * results in errors such as the following:
	 *
	 * Error: Unable to load resource module from http://aaronhardy.com/locales/en_US.swf
	 * Error: Unable to load style(SWF is not a loadable module): http://aaronhardy.com/fonts/Astroid.swf.
	 *
	 * Be aware that any module imported with this class may have access to anything within
	 * your application's security sandbox.
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ModuleMarshaller <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Appears to need to be stored in the class scope, otherwise garbage collection
		 * wipes out some of the event handling within IModuleInfo.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> module:IModuleInfo;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Constructor.
		 * @param url The external module to load into the application.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ModuleMarshaller<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">url</span> = <span style="color: #0066CC;">url</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Starts the loading process by loading in the module as bytes.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadModule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:URLLoader
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> urlRequest:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> urlLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span>urlRequest<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, bytesLoadedHandler<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SecurityErrorEvent.<span style="color: #006600;">SECURITY_ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> urlLoader;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Once the module bytes are loaded, convert them back into a module within the
		 * application's security domain.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> bytesLoadedHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> styleModuleBytes:ByteArray = ByteArray<span style="color: #66cc66;">&#40;</span>URLLoader<span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			module = ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #006600;">READY</span>, modReadyHandler<span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #0066CC;">ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, styleModuleBytes<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Once the module information is loaded, use the factory to create an instance of the
		 * module.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> modReadyHandler<span style="color: #66cc66;">&#40;</span>event:ModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Reports errors that may have occurred.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> errorHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event is ModuleEvent<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event is ErrorEvent<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModuleEvent<span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #0066CC;">ERROR</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>,
					ErrorEvent<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you&#8217;re loading a style module, you may also want this class, which ensures the style module takes effect in your application immediately after the module is loaded.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">aaronhardy</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">ModuleEvent</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Provides the module marshalling functionality of ModuleMarshaller with the ability to
	 * update styles immediately after the module is loaded.
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StyleModuleMarshaller <span style="color: #0066CC;">extends</span> ModuleMarshaller
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		 * If true, forces an immediate update of the application's styles after the module is
		 * loaded.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> updateStylesImmediately:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Constructor.
		 * @param url The external module to load into the application.
		 * @param update If true, forces an immediate update of the application's styles after
		 *        the module is loaded.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StyleModuleMarshaller<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>, updateStylesImmediately:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">updateStylesImmediately</span> = updateStylesImmediately;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * @inheritDoc
		 */</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> modReadyHandler<span style="color: #66cc66;">&#40;</span>event:ModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>updateStylesImmediately<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">Singleton</span>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mx.styles::IStyleManager2&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">styleDeclarationsChanged</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>The Dirty Details</h3>
<p>So, why does the ModuleMarshaller function without errors while Flex&#8217;s built-in classes do not?  You might not care, but in case you do, here&#8217;s what I know&#8211;and don&#8217;t know.  First, all the built-in functions end up using ModuleInfo.load() found in ModuleManager.as.  This function uses a Loader to load in the contents of the module.  When the loaded SWF file is accessible and ready for use (i.e., the Loader&#8217;s LoaderInfo object dispatches an Event.INIT event), ModuleInfo.initHandler() is called.  In this function we have the following segment of code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">factoryInfo = <span style="color: #000000; font-weight: bold;">new</span> FactoryInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">try</span>
<span style="color: #66cc66;">&#123;</span>
    factoryInfo.<span style="color: #006600;">factory</span> = loader.<span style="color: #006600;">content</span> as IFlexModuleFactory;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">error</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>factoryInfo.<span style="color: #006600;">factory</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> moduleEvent:ModuleEvent = <span style="color: #000000; font-weight: bold;">new</span> ModuleEvent<span style="color: #66cc66;">&#40;</span>
        ModuleEvent.<span style="color: #0066CC;">ERROR</span>, event.<span style="color: #006600;">bubbles</span>, event.<span style="color: #006600;">cancelable</span><span style="color: #66cc66;">&#41;</span>;
    moduleEvent.<span style="color: #0066CC;">bytesLoaded</span> = <span style="color: #cc66cc;">0</span>;
    moduleEvent.<span style="color: #0066CC;">bytesTotal</span> = <span style="color: #cc66cc;">0</span>;
    moduleEvent.<span style="color: #006600;">errorText</span> = <span style="color: #ff0000;">&quot;SWF is not a loadable module&quot;</span>;
    dispatchEvent<span style="color: #66cc66;">&#40;</span>moduleEvent<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Notice how it takes the content from the loader and attempt to cast it as an IFlexModuleFactory object.  In this case, loader.content is NOT an IFlexModuleFactory instance, resulting in factoryInfo.factory being set to null.  Later in the function, it checks to see if factoryInfo.factory is null.  If it is, an error is thrown.  This is the origin of the errors you see in your application.</p>
<p>So why does ModuleMarshaller work?  When ModuleMarshaller makes this call:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">module.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, styleModuleBytes<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It ultimately ends up in the same ModuleInfo.initHandler() function as we saw above.  In both cases the Flex Builder debugger shows that loader.content is of type GeneratedResourceModule1975496797516746634_mx_core_FlexModuleFactory.  However, when we load the module using the raw bytes loaded with a URLLoader, it IS able to cast the object as an IFlexModuleFactory object, resulting in a non-null factoryInfo.factory object and avoiding any error.</p>
<p>Why such similarity yet one is able to be cast while the other is not?  I don&#8217;t know.  That&#8217;s as far as I can go in my debugging quests with what&#8217;s open source.  I assume it has to do with the <a href="http://www.adobe.com/devnet/flashplayer/articles/flash_player_9_security.pdf" target="_blank">Flash Player security</a> but I haven&#8217;t read anything that would specifically explain why the content cannot be cast as an IFlexModuleFactory object when using built-in Flex module loading methods.  If you can fill in the gap or have questions, please feel free to post a comment.  Bon appetit!</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Loading a Remote Style SWF</title>
		<link>http://aaronhardy.com/flex/loading-a-remote-style-swf/</link>
		<comments>http://aaronhardy.com/flex/loading-a-remote-style-swf/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 02:32:32 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[IModuleInfo]]></category>
		<category><![CDATA[loadStyleDeclarations]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[StyleManager]]></category>
		<category><![CDATA[swf]]></category>
		<category><![CDATA[SWF is not a loadable module]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=206</guid>
		<description><![CDATA[This post has been replaced by Loading a Remote Module into a Local App. Enjoy!]]></description>
			<content:encoded><![CDATA[<p>This post has been replaced by <a href="/flex/loading-a-remote-module-into-a-local-app/">Loading a Remote Module into a Local App</a>.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/loading-a-remote-style-swf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

