Skip to content

Instantly share code, notes, and snippets.

@brian428
Created April 29, 2010 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brian428/383055 to your computer and use it in GitHub Desktop.
Save brian428/383055 to your computer and use it in GitHub Desktop.
<swiz:BeanProvider
xmlns:swiz="http://swiz.swizframework.org" xmlns:service="org.swizframework.quickswiz.service.*"
xmlns:controller="org.swizframework.quickswiz.controller.*">
<service:UserService id="userService"/>
<controller:UserController id="userController"/>
<!-- We'll use the Swiz ServiceHelper to help simulate a server-side call. -->
<swiz:ServiceHelper id="serviceHelper" />
</swiz:BeanProvider>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:view="org.swizframework.quickswiz.view.*" xmlns:config="org.swizframework.quickswiz.config.*"
xmlns:swiz="http://swiz.swizframework.org">
<fx:Declarations>
<swiz:Swiz>
<!-- BeanProviders simply contain the non-display objects that Swiz should process. -->
<swiz:beanProviders>
<config:Beans />
</swiz:beanProviders>
<swiz:loggingTargets>
<swiz:SwizTraceTarget id="myTraceTarget" />
</swiz:loggingTargets>
<swiz:config>
<!-- The eventPackages value tells Swiz the path to your Event classes,
and viewPackages is an optional value that speeds up the processing of display classes. -->
<swiz:SwizConfig
eventPackages="org.swizframework.quickswiz.event.*"
viewPackages="org.swizframework.quickswiz.view.*" />
</swiz:config>
</swiz:Swiz>
</fx:Declarations>
<s:layout>
<s:VerticalLayout horizontalAlign="center" paddingTop="20" />
</s:layout>
<view:UserForm id="userForm" />
</s:Application>
package org.swizframework.quickswiz.controller
{
import org.swizframework.quickswiz.model.User;
import org.swizframework.quickswiz.service.UserService;
public class UserController
{
[Inject]
public var userService : UserService;
[Bindable]
public var currentUser : User;
}
}
package org.swizframework.quickswiz.controller
{
import org.swizframework.quickswiz.model.User;
import org.swizframework.quickswiz.service.UserService;
public class UserController
{
[Inject]
public var userService : UserService;
[Bindable]
public var currentUser : User;
[PostConstruct]
/**
* [PostConstruct] methods are invoked after all dependencies are injected.
* In this example, we set up a default user after the bean is created.
*/
public function createDefaultUser() : void
{
currentUser = new User();
}
[EventHandler( event="UserEvent.SAVE_USER_REQUESTED", properties="user" )]
/**
* Perform a server request to save the user
*/
public function saveUser( user : User ) : void
{
// TODO: Add server interaction
}
}
}
package org.swizframework.quickswiz.controller
{
import org.swizframework.quickswiz.model.User;
import org.swizframework.quickswiz.service.UserService;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import org.swizframework.utils.services.ServiceHelper;
public class UserController
{
[Inject]
public var userService : UserService;
[Inject]
public var serviceHelper : ServiceHelper;
[Bindable]
public var currentUser : User;
[PostConstruct]
/**
* [PostConstruct] methods are invoked after all dependencies are injected.
* In this example, we set up a default user after the bean is created.
*/
public function createDefaultUser() : void
{
currentUser = new User();
}
[EventHandler( event="UserEvent.SAVE_USER_REQUESTED", properties="user" )]
/**
* Perform a server request to save the user
*/
public function saveUser( user : User ) : void
{
serviceHelper.executeServiceCall( userService.saveUser( user ), handleSaveUserResult );
}
/**
* Handle the server call result
*/
private function handleSaveUserResult( event : ResultEvent ) : void
{
// Show an Alert just to make it obvious that the save was successful.
Alert.show( 'User saved successfully!' );
}
}
}
package org.swizframework.quickswiz.event
{
import org.swizframework.quickswiz.model.User;
import flash.events.Event;
public class UserEvent extends Event
{
public static const SAVE_USER_REQUESTED : String = "saveUser";
public var user : User;
/**
* This is just a normal Flex event. The only thing to note is that we set 'bubbles' to true,
* so that the event will bubble up the display list, allowing Swiz to listen for your events.
*/
public function UserEvent( type:String )
{
super( type, true );
}
}
}
<s:Panel
xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import org.swizframework.quickswiz.event.UserEvent;
import org.swizframework.quickswiz.model.User;
[Bindable]
[Inject( source="userController.currentUser", bind="true" )]
/**
* We could inject the whole controller instance, but we only need
* one property from the controller, the current user, so we just
* inject that property.
*/
public var user : User;
]]>
</fx:Script>
<mx:Form>
<mx:Text id="userId" text="{isNaN( user.id ) ? 'N/A' : user.id}" />
<s:TextInput id="firstName" text="{user.firstName}" />
<s:TextInput id="lastName" text="{user.lastName}" />
<s:TextInput id="email" text="{user.email}" />
<s:Button label="Save" />
</mx:Form>
</s:Panel>
<s:Panel
xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import org.swizframework.quickswiz.event.UserEvent;
import org.swizframework.quickswiz.model.User;
[Bindable]
[Inject( source="userController.currentUser", bind="true" )]
/**
* We could inject the whole controller instance, but we only need
* one property from the controller, the current user, so we just
* inject that property.
*/
public var user : User;
/**
* Handle the user hitting the save button. We capture the form data
* and dispatch a standard Flex event. No Swiz-specific events or
* special central dispatcher needed!
*/
private function saveUser() : void
{
user.firstName = firstName.text;
user.lastName = lastName.text;
user.email = email.text;
var event : UserEvent = new UserEvent( UserEvent.SAVE_USER_REQUESTED );
event.user = user;
dispatchEvent( event );
}
]]>
</fx:Script>
<mx:Form>
<mx:Text id="userId" text="{isNaN( user.id ) ? 'N/A' : user.id}" />
<s:TextInput id="firstName" text="{user.firstName}" />
<s:TextInput id="lastName" text="{user.lastName}" />
<s:TextInput id="email" text="{user.email}" />
<s:Button label="Save" click="saveUser()" />
</mx:Form>
</s:Panel>
package org.swizframework.quickswiz.service
{
import org.swizframework.quickswiz.model.User;
import flash.events.IEventDispatcher;
import mx.rpc.AsyncToken;
import org.swizframework.utils.services.MockDelegateHelper;
public class UserService
{
[Dispatcher]
/**
* The [Dispatcher] metadata tag instructs Swiz to inject an event dispatcher.
* Events dispatched via this dispatcher can trigger methods annotated with [EventHandler].
*/
public var dispatcher : IEventDispatcher;
/**
* To avoid a live server dependency, we use a Swiz
* helper class to let us create fake AsyncTokens
*/
private var mockHelper : MockDelegateHelper;
public function UserService()
{
mockHelper = new MockDelegateHelper();
}
/**
* In a real app, we'd invoke a RemoteObject, HTTPService, etc.
* For this simple example, we'll set a random ID on the User
* to simulate the process of saving a User.
*/
public function saveUser( user : User ) : AsyncToken
{
user.id = Math.round( Math.random() * 100 );
return mockHelper.createMockResult( user );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment