Skip to content

Instantly share code, notes, and snippets.

@brian428
Created June 29, 2010 02:02
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/456685 to your computer and use it in GitHub Desktop.
Save brian428/456685 to your computer and use it in GitHub Desktop.
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();
}
[Mediate( 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!' );
}
}
}
<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" />
</mx:Form>
</s:Panel>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment