A Lightning Look at Configuration
Configuring Swiz in your Flex or AIR application is very straightforward: declare Swiz
, define the beanProviders
and config
properties, and optionally define one or more loggingTargets
to view debugging messages. Here is an example:
<script src="http://gist.github.com/383055.js?file=quickswiz_main.xml"></script>
Non-visual components that you want Swiz to manage are defined in a BeanProvider
tag. Any beans that you define within the BeanProvider
are processed by Swiz for dependency injection and the creation of event mediators. In the following example, a UserService
and a UserController
are created:
<script src="http://gist.github.com/383055.js?file=quickswiz_beans.xml"></script>
The "Big Three": Dependency Injection, Event Handling, and Server Interaction
The three most commonly used features of Swiz are its dependency injection capabilities, its event handling features, and its server interaction utilities. Let's look at how each of these work.
Adding Dependency Injection
Dependencies are injected by using [Inject]
metadata. In this example, the UserService
is injected into the UserController
:
<script src="http://gist.github.com/383055.js?file=quickswiz_usercontroller.java"></script>
In addition to injecting a bean, you can inject individual bean properties. In this example, the currentUser
property of the UserController
is injected into a UserForm
visual component. Note that it is not necessary for the UserForm
to be declared as a bean in the BeanProviders
tag. When visual components are added to the display list, Swiz automatically inspects them and processes any metadata tags that are found.
<script src="http://gist.github.com/383055.js?file=quickswiz_userform.mxml"></script>
Dispatching and Handling Events
When using Swiz, you dispatch standard Flex events. In this example, we've expanded the UserForm
to create and dispatch an event when the form button is clicked. Swiz listens for events being dispatched within the display list and handles them automatically.
<script src="http://gist.github.com/383055.js?file=quickswiz_userform2.mxml"></script>
A look at the UserEvent
confirms that this is just a simple, standard event. The only thing to note here is that the event has bubbles
set to true
. This allows the event to bubble up the display list so that it can be handled by Swiz:
<script src="http://gist.github.com/383055.js?file=quickswiz_userevent.java"></script>
We've seen how to dispatch events from the display list, but how do we handle the events? Swiz provides a [Mediate]
metadata tag to do this. In the example below, we have expanded the UserController
to add a mediated method. In this case, when Swiz encounters an event of type UserEvent.SAVE_USER_REQUESTED
, it will automatically invoke the saveUser
method in the UserController
. By specifying a value of "user" for the properties
attribute, Swiz will locate the user
property in the UserEvent
and pass this as a parameter to the mediated method:
<script src="http://gist.github.com/383055.js?file=quickswiz_usercontroller2.java"></script>
One last feature to note regarding event dispatching. We've seen how an event dispatched from the display list will bubble up until it is processed by Swiz. But what about dispatching events from non-visual components like controllers and services? Swiz offers the [Dispatcher]
metadata tag to meet this need. In the UserService
shown below, Swiz will automatically inject an event dispatcher into the dispatcher
property. Any events dispatched though this dispatcher will also be processed by Swiz and trigger any mediated methods associated with the event:
<script src="http://gist.github.com/383055.js?file=quickswiz_userservice.java"></script>
Talking to the Server
<script src="http://gist.github.com/383055.js?file=quickswiz_usercontroller3.java"></script>