Tag Archive for: Flex

Swiz 1.0 Review

I have listed some of the popular features and basic differences between Swiz 0.6.2 and Swiz 1.0.0 RC1 and supplied some resources below:

Although the swc file size has grown from 28K to 66K, Swiz still provides punch-packing architecture conveniences that border on alchemy.

Swiz Config

The configuration has been simplified and improved:

<fx:Declarations>
<swiz:Swiz id="mySwiz">
<swiz:config>
<swiz:SwizConfig id="mySwizConfig"
eventPackages="com.package.events"
viewPackages="com.package.views"
defaultFaultHandler="{genericFault}"/>
</swiz:config>
<swiz:beanProviders>
<config:Beans/>
</swiz:beanProviders>
</swiz:Swiz>
</fx:Declarations>

You can now declare your eventPackages and viewPackages paths.

eventPackages – If you point eventPackages=’com.package.events’, then you can simply reference any event in that package with Class.CONSTANT in the Mediate’s “event” attribute. This allows you to set the actual string value in your Event Class to what ever you want.
Mediator looks like this: [Mediate( event=”MyEvent.SUBMIT”)]
Event constant can look like this: public static const MyEvent.SUBMIT:String = ‘whateva’;

viewPackages – This is an option but will speed up the processing of the display classes.

Beans File

The Beans file looks similar but now is wrapped in a BeanProvider tag:

<swiz:BeanProvider xmlns:swiz="http://swiz.swizframework.org"
xmlns:controller="com.jci.h2o.controllers.*"
xmlns:delegates="com.jci.h2o.delegates.*">
<fx:Declarations>
<controller:MyController id="myController" />
<delegates:MockDelegate id="mockDelegate" />
</fx:Declarations>
</swiz:BeanProvider>

Swiz Events

[Dispatcher]
public var dispatcher : IEventDispatcher;

  • No longer do you use Swiz.dispatchEvent instead create a [Dispatcher] and call dispatcher.dispatchEvent and dispatch a standard Flex Event (yay!)
  • On Event Classes that are to be dispatched by Swiz, set bubbles=true so that the event will bubble up the display list, allowing Swiz to listen
  • [Dispatch] allows you to “technically” dispatch events from non-visual components ( somehow it’s always worked with 0.6.4 although it was not supposed to ).

Dependency Injection

  • [Autowire] metadata is no longer used to inject dependencies – instead use [Inject]
  • Inject any individual property of a Bean by specifying the source. ( Injecting an entire bean (or Injection by Name) using the “source” attribute has been available for some time however, I believe Injection of bean properties is new ).

Injection by Type

[Inject]
public var userService : UserService;    // Injection by Type

– or –

Injection by Name

[Bindable]
[Inject( source="userController" )]    // Injection by Name

– or –

Injection by property

[Bindable]
[Inject( source="userController.currentUser" )]    // Injection by property

Two way Binding

Very cool. Two way binding is supported via the Inject’s twoWay attribute.

[Bindable]
[Inject( source="userModel.currentUser", twoWay="true" )]
public var currentUser:User;

Mediate Multiple Events from a Single Method

Booyah!!

[Mediate( event="UserEvent.ADD_USER", properties="user" )]
[Mediate( event="UserEvent.EDIT_USER", properties="user" )]
[Mediate( event="UserEvent.DELETE_USER", properties="user" )]
public function mediateUserEvents( user:User ):void
{
// do stuff
}

– or –

[Mediate( event="UserEvent.*", properties="user" )]
public function mediateUserEvents( user:User ):void
{
  // do stuff
}

Service Helper

Service Helper is similar to AbstractController, however there is a really cool feature where you can pass an array of parameters that are then passed to your result handler eliminating the need to create an extra private var to hold the value from the service caller method over to the result handler.

public function fetchUserRoles( user:User ):void
{
sh.executeServiceCall( ro.getUserRoles( user.id ), getUserRoles_result, getUserRoles_fault, [ user ] );
}
protected function fetchUserRoles_result( data:Object, user:User ):void
{
user.roles = data.result;
}

Post Construct

[PostConstruct] methods are invoked after all dependencies are injected – for example – setting up a default user after the bean is created.

[PostConstruct]
public function createDefaultUser() : void
{
currentUser = new User();
}

Resources

New Swiz Website:
http://swizframework.org

The Swiz wiki is very well done. I suggest reading the entire user guide on the wiki:
http://swizframework.jira.com/wiki/display/SWIZ/Home

Brian Kotek has put together a nice demo to explore the new features of Swiz here:
http://swizframework.jira.com/wiki/display/SWIZ/Quick+Start

Michael Schmalle has gone a little deeper here:
http://blog.teotigraphix.com/2010/05/21/flex-swiz-1-0rc-demo-under-the-hood/