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.

var swizConfig:SwizConfig = new SwizConfig();
swizConfig.eventPackages = [ "com.as3offcuts.as3Swiz.events" ];
swizConfig.viewPackages = [ "com.as3offcuts.as3Swiz.view" ];

var swiz:Swiz = new Swiz( this, swizConfig, null, [ Beans ] );
swiz.init();

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:

    package
    {
    	import com.as3offcuts.as3Swiz.controller.SomeController;
    	import com.as3offcuts.as3Swiz.model.SomeModel;
    
    	import org.swizframework.core.BeanProvider;
    
    	public class Beans extends org.swizframework.core.BeanProvider
    	{
    		private var _someModel:SomeModel;
    		public function get someModel( ):SomeModel
    		{
    			if( !_someModel )
    				_someModel = new SomeModel();
    
    			return _someModel;
    		}
    		public function set someModel( value:SomeModel ):void
    		{
    			_someModel = value;
    		}
    
    		private var _someController:SomeController;
    		public function get someController( ):SomeController
    		{
    			if( !_someController )
    				_someController = new SomeController();
    
    			return _someController;
    		}
    		public function set someController( value:SomeController ):void
    		{
    			_someController = value;
    		}
    
    		public function Beans(beans:Array=null)
    		{
    			super(beans);
    		}
    
    	}
    }
    

    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.

    Bookmark and Share

    Tags: ,

    3 Responses to “Swiz in Actionscript Projects (including Flash IDE projects)”

    1. Gareth Arch says:

      Hey Dave,
      This should help people setting up their AS Swiz projects.

      One thing I noticed:

      I think line 26 should be

      if( !_someController )

      rather than

      if( !_someModel )

    2. [...] Swiz in Actionscript Projects (including Flash IDE projects) Para quem tiver interesse em utilizar o Swiz Framework sem ser em um projeto Flex ou AIR é interessante a leitura desse post. [...]

    3. Dave says:

      Hi,

      Thanks for spotting that, I’ve updated the post to correct it.

      Dave.

    Leave a Reply