One of the very painful thing to do when working in AS is exposing a JS API for your application and/or have javascript code embedded in your swf application do something useful interacting with it.
Understanding the wtf
The flash API gives you access to two methods in order to communicate with the JS side of things.
The first one is ExternalInterface.addCallback, that will declare that a certain new javascript function bound onto the js object representing your swf node will, when called, call-back a specific actionscript method.
This mechanism unfortunately presents several drawbacks:
- extremely verbose and boring to use if you want to expose more than a couple of lousy functions
- forces you to maintain code to reflect your API changes on the JS side
- doesn't let you return complex types objects
- there is no simple way to know how to get a reference back to your swf node
- ExternalInterface.available lies
- debugging it can be very tedious
Why do that anyway?
Well, we are in a world where things have to communicate. Being able to produce a flash application that exposes a sane javascript API is a must and makes it possible to have flash as a drop-in replacement for features missing in a given browser, or at least propose your application in an inter-operable manner, making you a better, more open web-developer and less of an egoist flash-boy :-)
More seriously, bringing flash app up to par with javascript code modules certainly is a good way to make things move forward and can smooth integration processes greatly.
So, what do we need? An AS library that assumes the role of a full-duplex "bridge" between the two scopes.
I've seen recently that FlashBuilder proposes some kind of Ajax exposure generated at compile-time, but in my opinion this is weak:
- it's not self-contained, which means you need to embed in your web-page not only your swf, but also some additional js
- it doesn't address the shortcomings from above
Meet JSAS
JSAS is an AS3 library that let you expose entire AS objects, as described by an AS interface, onto a javascript namespaced object. A single call is enough to get your object mapped (no need to maintain one call per method, no need to name them explicitely). Features include:- exposure deals with interface inheritance
- simple type accessors (getters setters) are exposed as well
- complex type AS objects can be returned by AS methods calls
- complex type AS objects (in their JS form) can be passed back as arguments to AS methods calls
- complex type accessors are exposed as member objects of the parent js object, and exposed as well
- doesn't crash if AS objects are garbage collected, or if something goes wrong on the AS side
JSAS also provides the concept of an execution sandbox, that let you evaluate javascript (from AS) in a global scope that is bound to the "contentWindow" member of your swf node - cutting the headache to get a reference back to your object prior to do anything useful with it.
Finally, one may implement serialization methods in order to be able to pass complex type pure js objects through the JSAS layer, to AS methods.
Get JSAS
JSAS is provided under a GPL license.
The documentation page is here.
The mercurial repository is here.
Note that the JSAS library depends on rebase in order to compile.
1
ljouanneau
Sunday, December 20 2009, 12:18
ljouanneau's status on Sunday, 20-Dec-09 11:17:56 UTC
want to expose full AS3 objects into your web page ? JSAS is for you http://tinyurl.com/ydgpfok #zoomorama #opensource...