rcyeager
10-11-2006, 08:25 PM
I'm getting started optimizing my app for a small initial download size via dynamically imported libraries. I hit a snag with a SWF library and found a workaround after a few hours, so I thought I would share what I have learned.
I'm using Togawa's wonderful Yahoo! SWF library as an example.
The original static linking at compile time looked something like this:
<include href="/yahoomaps"/>
...
<ymapMap appid="XXX"/>
...
{inside of Togawa's yahoomap code:}
<class name="ymapMap" extends="view" resource="yahoomaps.swf">
To get dynamic loading working, the changes look like:
<import name="lib_yahoomap" href="yahoomaps/library.lzx" stage="defer"/>
...
{somewhere during init}
lib_yahoomap.load();
...
{somewhere after the lib loads}
mapView.setResource("http:yahoomaps/yahoomaps.swf");
mapView.completeInstantiation();
...
<ymapMap name="mapView" appid="XXX" initstage="defer"/>
...
{inside Togawa's code, first remove the resource}
<class name="ymapMap" extends="view">
and change *ALL* __LZmovieClipRef references to add __LZmovieClipRef.lmc
The first part is easy...change the include into a deferred import of the library code.
The second part is easy, but has a couple of tricks. Change the map instance to have deferred instantiation, so that you can configure the resource. In my case, I have subviews within the map view...which prevents changing the resource through script (a runtime error occurs saying this). Another trick is the http: in front of the resource name. You cannot link to resources across a domain, probably due to the Flash security and the crossdomain file configuration. Unlike a file-based library include, you have to include the http: in front of the reference for the resource to load.
The final changes inside Togawa's library wrapper itself are to remove the static resource definition. But here was the main trick. The __LZmovieClipRef reference no longer points to the loaded yahoomaps.swf library resource, but rather to a LZMediaLoader object. This loader object performs the dynamic load, with the target SWF ending up in the "lmc" attribute. To access the APIs within the SWF then requires references to __LZmovieClipRef.lmc.{api call}.
This technique should work for the DENG SWF library and others.
Hope this helps someone down the road.
Robert
I'm using Togawa's wonderful Yahoo! SWF library as an example.
The original static linking at compile time looked something like this:
<include href="/yahoomaps"/>
...
<ymapMap appid="XXX"/>
...
{inside of Togawa's yahoomap code:}
<class name="ymapMap" extends="view" resource="yahoomaps.swf">
To get dynamic loading working, the changes look like:
<import name="lib_yahoomap" href="yahoomaps/library.lzx" stage="defer"/>
...
{somewhere during init}
lib_yahoomap.load();
...
{somewhere after the lib loads}
mapView.setResource("http:yahoomaps/yahoomaps.swf");
mapView.completeInstantiation();
...
<ymapMap name="mapView" appid="XXX" initstage="defer"/>
...
{inside Togawa's code, first remove the resource}
<class name="ymapMap" extends="view">
and change *ALL* __LZmovieClipRef references to add __LZmovieClipRef.lmc
The first part is easy...change the include into a deferred import of the library code.
The second part is easy, but has a couple of tricks. Change the map instance to have deferred instantiation, so that you can configure the resource. In my case, I have subviews within the map view...which prevents changing the resource through script (a runtime error occurs saying this). Another trick is the http: in front of the resource name. You cannot link to resources across a domain, probably due to the Flash security and the crossdomain file configuration. Unlike a file-based library include, you have to include the http: in front of the reference for the resource to load.
The final changes inside Togawa's library wrapper itself are to remove the static resource definition. But here was the main trick. The __LZmovieClipRef reference no longer points to the loaded yahoomaps.swf library resource, but rather to a LZMediaLoader object. This loader object performs the dynamic load, with the target SWF ending up in the "lmc" attribute. To access the APIs within the SWF then requires references to __LZmovieClipRef.lmc.{api call}.
This technique should work for the DENG SWF library and others.
Hope this helps someone down the road.
Robert