Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

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:

Unknown macro: {html}

<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:

Unknown macro: {html}

<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:

Unknown macro: {html}

<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.

Unknown macro: {html}

<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.

Unknown macro: {html}

<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:

Unknown macro: {html}

<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:

Unknown macro: {html}

<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:

Unknown macro: {html}

<script src="http://gist.github.com/383055.js?file=quickswiz_userservice.java"></script>

Talking to the Server

Our UserController can now respond when a save user event is dispatched. The final piece of the puzzle is having the application make a call to the server to actually save the user in a database. Typically, Flex requries you to manually create Responder objects to attach to AsyncTokens that will handle ResultEvent and FaultEvent. Swiz offers some helpful features to sidestep these manual processes. Below, you see the final version of our UserController. Because it extends the Swiz AbstractController, we inherit the executeServiceCall() method. This method will automatically obtain the AsyncToken for the RPC call and create Responder objects. In this case, we call userService.saveUser(), and specify handleSaveUserResult to handle the successful response from the server. Although it is not shown here, we can also specify a method to use as the fault handler.

Unknown macro: {html}

<script src="http://gist.github.com/383055.js?file=quickswiz_usercontroller3.java"></script>


There's More Where That Came From

This quick tour has shown how easy it is to configure Swiz and use its core features. But Swiz offers much more than this! Please read the User Guide, Best Practices, and Advanced Topics to learn more.

  • No labels