Swiz in plain AS3 projects

23.10.2009

After working with swiz for two projects I am allready at the point where I don't want to miss it again.
For a current customer though, my colleague and I needed to code a raw ActionScript project and guess what.. we where not at all amused not to have swiz on our sides ;-)
This is where I decided to take a look and see if I can get swiz to work on plain AS3 projects.

After some hours of work the result was there Surprisingly fast...

The Changes

here is what was modified:

  • SwizConfig has an init function now, which needs the main Class of the application as an argument.
  • The default injection event was changed back to addedToStage
  • view-autowiring is done directly after a view was added to the stage (the original swiz waits for the creationComplete event for this to happen)
  • The BeanLoader now has an abstract function 'initBeans' in which you can just put your beans into a dictionary.

As you can see, the modifications are less than minor. The only drawback is that I could not get rid of all mx.* dependencies (approximately 80kb increase in filesize) but I think most people could live with it.

Get It

To download our modified version of swiz just grab your favorite subversion client, and check it out from svn://negdis.com/swiz/Trunk/src

In This folder you will also find an example project showing how to get started.

How To Start in 5 easy steps

If you want to start your own pure ActionScript project with swiz, here is what you will have to do:

1. As with Flex, add swiz-specific metadata into your project properties

add swiz metadata

2. Further add the Flex dependencies..

you get them by adding the /framework/libs and the /frameworks/locale/en_US folders from the Flex sdk as an swc-folder.

add flex dependencies

3. Initializing Swiz

Before adding anything to the stage, you will have to initialize and start swiz with SwizConfig:

  1.  
  2. sConfig = new SwizConfig();
  3. sConfig.beanLoaders = [Beans];
  4. sConfig.eventPackages = "just.an.example.events";
  5. sConfig.mediateBubbledEvents = true;
  6. sConfig.viewPackages = "just.an.example.view";
  7. sConfig.strict = true;
  8. sConfig.init(this);
  9.  

4. Register you beans

You get your beans to work by extending the BeanLoader, overriding the function initBeans and assigning them to the provided property 'beans':

  1.  
  2. package just.an.example.beans
  3. {
  4. import just.an.example.controller.MyController;
  5. import just.an.example.model.MyModel;
  6. import org.swizframework.util.BeanLoader;
  7.  
  8. public class Beans extends BeanLoader
  9. {
  10. override protected function initBeans():void
  11. {
  12. beans['myController'] = new MyController();
  13. beans['myModel'] = new MyModel();
  14. }
  15. }
  16. }
  17.  
5. Rock your project!

Everything else should work as expected. Mediation of events, autowiring of beans and views.. you won't even loose the rich extended binding features of swiz :-) 

Final Words

We know that the swiz guys are currently working on a pure AS version of their framework and we can't wait for it to be released. For those of you who can't wait too, this could be a good temporary solution ;-)

 

2 responses to "Swiz in plain AS3 projects"

12.01.2010 | guille Says:
Hey Ben, That's great news!
Can't wait for it to be stable :-)
16.12.2009 | Ben Clinkinbeard Says:
Wow, not sure how we missed this back when it was posted. Google Alerts FTL. Anyhow, in case you missed it we launched the 1.0.0 alpha release on Monday, and it includes pure AS3 support. The changes you made are almost identical to what we essentially did. Glad you like the framework, be sure to give us your thoughts on the latest! http://swizframework.org/2009/12/swiz-1-0-0-alpha-released/

Post a response: