Hello group,
I've just completed porting a relatively large library from v2's
util lib: ArcGISLink. Although it's an interface lib to a commercial
product, it can still might be of interest to other lib developers in
several aspects:
---- Like many other well known js libs, the code is wrapped in an
anonymous function scope to hide all private functions variables, in
order to protect the globe namespace. This also helps the JS
compressors to shorten the private names.
--- I picked namespace "gmaps", which is simple and easy to
understand. Also a sub namespace "ags" to contain all public classes
inside the lib. If there is a different namespace everyone agrees for
this project, I can change that.
--- This lib heavily uses a couple classes that are less commonly
customized: Projection, MapType, it also added the TileLayer concept
in v2 to handle multiple tilesets per map type. Other developers may
find some of the code helpful to work with this type of
customization.
--- I spent a great deal of time on the closure compiler. (Thanks
Chad Killingworth for contributing the extern file). Simple
optimization is quite straightforward. As to the advanced
optimization, exporting class is easy, followed by exporting public
methods. Since the compiler does not rename strings, there is a trick
to avoid exporting methods in extra code using this syntax:
MyClass.prototype["myMethod"] = MyClass.prototype.myMethod; instead,
write the method directly using this syntax:
MyClass.prototype["meMethod"] = function(){...}; The downside is
compiler complains about "this" because it does not know it's a
prototype method, also current JSDOC does not reorganize it (maybe
tweak the JSDoc tool can help).
The nightmare came with the property names along with heavy usage
of json in my lib. In my opinion the compiler should not rename
properties if it is not declared in a class with "this". Unfortunately
the compiler choose to rename everything that is not in subscript
notation. That means I have to change many code from dot notation to
subscript notation. When all was done, I found the code is much less
readable than original.
Finally I compiled the code with Dean's packer, and closure compiler
with simple and advanced optimization. The original size is 130K,
Packer 24K, Simple Op 29K, Advanced 27K. When served with gzip
(Hopefully
googlecode.com can gzip js files some time), the size were
12.4, 11.5, 10.9K, respectively.
My personal feeling after the comparison of all three option is that
the simple optimization is the best choice considering it's quite safe
and also does a pretty good job in compressing. It may be different if
the lib does not use many external json objects.
--- Most sample code uses full screen as to fit mobile device.
samples are tested on Nexus one, and most work except a few issues
that may related to Android browser. I also made a "phone size"
version of most examples to kind of fake a phone screen.
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/arcgislink/examples/nexus.html?u=mercator.html.
Although the actual device got more screen pixel because of higher
DPI, this is a fun experiment.
The example index page is at
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/arcgislink/docs/examples.html,
one interesting one is routing with barriers.
Comments welcome,
Nianwei