Swiz in Actionscript Projects (including Flash IDE projects)

I've been spending a bit of time with Swiz 1.0 and thought I would see how easy it was to get it working with actionscript projects. Most of the documentation focuses on how to configure Swiz in Flex 3 and 4, but not how to do so in pure actionscript.

Turns out its actually pretty easy: either in Flex Builder or the Flash IDE. The only caveat to it being truely pure actionscript is that you need to use Flex's framework.swc in order to let Swiz use data binding, and rpc.swc to use some of Swiz's service classes.

The first step is to setup the project. For this you'll need to download a copy of Swiz and a copy of the Flex SDK. Then setup a new project and add the Swiz swc (e.g. swiz-framework-1.0.0-RC1), and Flex's framework.swc (found in flex_sdk_dir/frameworks/libs/) to the project's library path.

By using the Swiz swc there is no need to add the -keep-as3-metadata compiler argument. If you're using the Flash IDE, then you'll need to turn on the 'Export SWC' option in the Publish Settings' flash menu. This is a little workaround that stops Flash stripping the metadata out of your code. ( Thanks to Patrick Mowrer for that ).

Next step is to configure Swiz. This is usually done in MXML in a Flex project, but can just as easily be done in actionscript.

To begin with we create a new SwizConfig instance, and set its eventPackages and viewPackages properties to match our package structure. There are a lot of other properties we could set here, but there's no need for a normal project.

Next we create a Swiz instance, passing it:

  • An EventDispatcher. One high up on the display list to catch views being added, and bubbling events they dispatch.
  • Our config instance.
  • An array of BeanProviders (or BeanLoaders), either as instances or class references. More on this below.
  • We then call init on our Swiz instance, and that's it, we're off.

    Stepping back a second though, we needed a BeanProvider to pass to Swiz in the previous step. An actionscript BeanProvider looks something like this:

    Essentially just defining a getter/setter for each bean is all that's needed. There are other ways to do this, but I won't go into details here.

    That's all there is to it. You're now set to start using Swiz in your project. I've included a very basic skeleton project that has both a Flex Builder and Flash IDE version. To use them you'll need to change the library path locations for framework.swc so they point to wherever you have your flex sdk installed. The project doesn't do very much, just trace out statements showing that Swiz is working the way it should.

    Actionscript Swiz example.

    2 responses
    Thanks for this article! This information was just what I needed. I'm having an issue with my views not being recognized, though. I have classes assigned to visual items within Flash that are within my view package structure, but they don't seem to get processed by SWIZ. Have you tried this?
    Hi.

    This isn't something I've come across myself.

    Do you have a project that shows the problem that I could see?