Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changed login example to match. Sleeeepy

  • Loading branch information...
commit 6296b34c6ab27afbcb4c4030fe2114527bf0a994 1 parent 571e0b5
@seanhess authored
View
64 examples/GlueLoginExample/src/login/control/Authentication.as
@@ -1,64 +0,0 @@
-package login.control
-{
- import flash.events.Event;
-
- import login.model.event.LoginExample;
- import login.service.IAuthenticationService;
-
-
- /**
- * Controllers responsible for providing application-level
- * functionality. They provide a set of funtions that can
- * be called to do anything in the system.
- *
- * They then manipulate the data.
- *
- * I have the logic for the services in a separate file,
- * because I want to be able to swap out service
- * implementations and still test this one.
- */
-
- [Bindable]
- public class Authentication
- {
- public var authenticated:Boolean = false;
- public var email:String;
-
- /**
- * implementation is injected from the map
- */
- public var authService:IAuthenticationService;
-
- public function login(username:String, password:String):void
- {
- authService.addEventListener(LoginExample.LOGIN, onLogin);
- authService.login(username, password);
- }
-
- private function onLogin(event:Event):void
- {
- authenticated = authService.authenticated;
- email = authService.email; // or in the result or something...
-
- if (authenticated)
- dispatchEvent(new Event(LoginExample.LOGIN));
-
- else
- dispatchEvent(new Event(LoginExample.FAIL));
- }
-
- public function logout():void
- {
- authService.addEventListener(LoginExample.LOGOUT, onLogout);
- authService.logout();
- }
-
- private function onLogout(event:Event):void
- {
- authenticated = false;
- email = null;
- dispatchEvent(new Event(LoginExample.LOGOUT));
- }
-
- }
-}
View
69 examples/GlueLoginExample/src/login/control/Authentication.mxml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="utf-8"?>
+<mx:Object xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:model="login.model.*" xmlns:service="login.service.*">
+
+ <model:Application id="app"/>
+ <model:Navigation id="nav"/>
+
+ <mx:HTTPService id="loginService" url="../server/login.php" resultFormat="object" result="onLogin(event)">
+ <mx:request>
+ <username>{currentAttempt.username}</username>
+ <password>{currentAttempt.password}</password>
+ </mx:request>
+ </mx:HTTPService>
+
+ <mx:HTTPService id="logoutService" url="../server/logout.php" resultFormat="object" result="onLogout(event)">
+ </mx:HTTPService>
+
+ <service:ParseAuthResponses id="parser"/>
+
+ <mx:Script>
+ <![CDATA[
+ import mx.rpc.events.ResultEvent;
+ import login.model.Login;
+ import login.model.User;
+
+ [Bindable] public var currentAttempt:Login;
+
+ public function login(value:Login):void
+ {
+ currentAttempt = value;
+ currentAttempt.failed = false;
+ currentAttempt.waiting = true;
+ loginService.send();
+ }
+
+ public function logout():void
+ {
+ logoutService.send();
+ }
+
+ /**
+ * This is some logic, but it's testable by mocking the services
+ */
+ private function onLogin(event:ResultEvent):void
+ {
+ currentAttempt.waiting = false;
+ var user:User = parser.parseLoginResponse(event.result);
+
+ if (user.valid)
+ {
+ app.currentUser = user;
+ nav.loggedIn();
+ }
+ else
+ {
+ currentAttempt.failed = true;
+ onLogout(null);
+ }
+ }
+
+ private function onLogout(event:Event):void
+ {
+ currentAttempt = null;
+ app.currentUser = null;
+ nav.loggedOut();
+ }
+ ]]>
+ </mx:Script>
+
+</mx:Object>
View
28 examples/GlueLoginExample/src/login/glue/MainGlue.mxml
@@ -1,32 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
-<GlueMap xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="http://glue.seanhess.net/2009" xmlns:control="login.control.*" xmlns:service="login.service.*" xmlns:view="login.view.*">
+<GlueMap xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="http://glue.seanhess.net/2009" xmlns:control="login.control.*" xmlns:service="login.service.*" xmlns:view="login.view.*" xmlns:model="login.model.*">
<mx:Script>
<![CDATA[
- import login.model.event.LoginExample;
+ import login.model.Login;
]]>
</mx:Script>
- <service:MockAuthenticationService id="mockAuthService"/>
- <service:AuthenticationService id="authService"/>
+ <!-- MODELS -->
+ <model:Application id="app"/>
+ <model:Navigation id="nav"/>
- <Glue>
- <control:Authentication id="auth"
- authService="{mockAuthService}"
- />
- </Glue>
+
+ <!-- CONTROLLERS -->
+ <control:Authentication id="auth" app="{app}" nav="{nav}"/>
<Glue>
<view:MainView id="mainView"/>
- <Inject userEmail="{auth.email}"/>
-
- <Route event="{LoginExample.LOGIN}" call="auth.login(mainView.username, mainView.password)"/>
- <Route event="{LoginExample.LOGOUT}" call="auth.logout()"/>
+ <Inject user="{app.currentUser}"/>
+ <Inject nav="{nav}"/>
- <Observe controller="{auth}" event="{LoginExample.LOGIN}" call="mainView.hideLogin()"/>
- <Observe controller="{auth}" event="{LoginExample.LOGOUT}" call="mainView.showLogin()"/>
- <Observe controller="{auth}" event="{LoginExample.FAIL}" call="mainView.failLogin()"/>
+ <Route event="{Login.LOGIN}" call="auth.login(mainView.login)"/>
+ <Route event="{Login.LOGOUT}" call="auth.logout()"/>
</Glue>
</GlueMap>
View
8 examples/GlueLoginExample/src/login/model/Application.as
@@ -0,0 +1,8 @@
+package login.model
+{
+ [Bindable]
+ public class Application
+ {
+ public var currentUser:User;
+ }
+}
View
24 examples/GlueLoginExample/src/login/model/Login.as
@@ -0,0 +1,24 @@
+package login.model
+{
+ [Bindable]
+ public class Login
+ {
+ public static const LOGIN:String = "login";
+ public static const LOGOUT:String = "logout";
+ public static const FAIL:String = "fail";
+
+ public var username:String;
+ public var password:String;
+
+ public var waiting:Boolean = false;
+ public var failed:Boolean = false;
+
+ public function reset():void
+ {
+ username = "";
+ password = "";
+ waiting = false;
+ failed = false;
+ }
+ }
+}
View
25 examples/GlueLoginExample/src/login/model/Navigation.as
@@ -0,0 +1,25 @@
+package login.model
+{
+ import flash.events.EventDispatcher;
+
+ [Bindable]
+ public class Navigation extends EventDispatcher
+ {
+ public static const LOGGED_OUT:String = "loggedOut";
+ public static const LOGGED_IN:String = "loggedIn";
+
+ public var showLogin:Boolean = true;
+
+ public function loggedIn():void
+ {
+ showLogin = false;
+ dispatchEvent(new Event(LOGGED_IN));
+ }
+
+ public function loggedOut():void
+ {
+ showLogin = true;
+ dispatchEvent(new Event(LOGGED_OUT));
+ }
+ }
+}
View
9 examples/GlueLoginExample/src/login/model/User.as
@@ -0,0 +1,9 @@
+package login.model
+{
+ [Bindable]
+ public class User
+ {
+ public var email:String;
+ public var valid:Boolean = true;
+ }
+}
View
12 examples/GlueLoginExample/src/login/model/event/LoginExample.as
@@ -1,12 +0,0 @@
-package login.model.event
-{
- /**
- * This class just has event constants in it
- */
- public class LoginExample
- {
- public static const LOGIN:String = "login";
- public static const LOGOUT:String = "logout";
- public static const FAIL:String = "fail";
- }
-}
View
56 examples/GlueLoginExample/src/login/service/AuthenticationService.mxml
@@ -1,56 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<events:EventDispatcher
- xmlns:mx="http://www.adobe.com/2006/mxml"
- xmlns:events="flash.events.*"
- implements="login.service.IAuthenticationService"
- >
-
- <mx:Script>
- <![CDATA[
- import login.model.event.LoginExample;
- import mx.rpc.events.ResultEvent;
-
- private var _authenticated:Boolean;
- private var _email:String;
-
- public function get authenticated():Boolean
- {
- return _authenticated;
- }
-
- public function get email():String
- {
- return _email;
- }
-
- public function login(username:String, password:String):void
- {
- loginService.send({
- username:username,
- password:password
- });
- }
-
- private function onLogin(event:ResultEvent):void
- {
- _authenticated = event.result.response.authenticated;
- _email = event.result.response.email;
- dispatchEvent(new Event(LoginExample.LOGIN));
- }
-
- public function logout():void
- {
- logoutService.send();
- }
-
- private function onLogout(event:ResultEvent):void
- {
- dispatchEvent(new Event(LoginExample.LOGOUT));
- }
- ]]>
- </mx:Script>
-
- <mx:HTTPService id="loginService" url="../server/login.php" resultFormat="object" result="onLogin(event)"/>
- <mx:HTTPService id="logoutService" url="../server/logout.php" resultFormat="object" result="onLogout(event)"/>
-
-</events:EventDispatcher>
View
16 examples/GlueLoginExample/src/login/service/IAuthenticationService.as
@@ -1,16 +0,0 @@
-package login.service
-{
- import flash.events.IEventDispatcher;
-
- /**
- * There are two problems with this approach.
- *
- */
- public interface IAuthenticationService extends IEventDispatcher
- {
- function login(username:String, password:String):void;
- function logout():void;
- function get email():String;
- function get authenticated():Boolean;
- }
-}
View
21 examples/GlueLoginExample/src/login/service/ParseAuthResponses.as
@@ -0,0 +1,21 @@
+package login.service
+{
+ import login.model.User;
+
+ public class ParseAuthResponses
+ {
+ public function parseLoginResponse(value:Object):User
+ {
+ var user:User = new User();
+ user.email = value.response.email;
+ user.valid = value.response.authenticated;
+
+ return user;
+ }
+
+ public function parseLogoutResponse(value:Object):Boolean
+ {
+ return true;
+ }
+ }
+}
View
61 examples/GlueLoginExample/src/login/view/MainView.mxml
@@ -1,66 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
-<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
-
- <mx:Metadata>
- [Event("login")]
- [Event("logout")]
- </mx:Metadata>
+<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" xmlns:model="login.model.*">
<mx:Script>
<![CDATA[
- import login.model.event.LoginExample;
-
- [Bindable] public var loginVisible:Boolean = true;
- [Bindable] public var userEmail:String;
+ import login.model.Navigation;
+ import login.model.User;
- [Bindable] public var wait:Boolean = false;
- [Bindable] public var failed:Boolean = false;
+ [Bindable] public var user:User;
+ [Bindable] public var nav:Navigation;
- public function hideLogin():void
+ private function onLoginClick():void
{
- loginVisible = false;
+ dispatchEvent(new Event(Login.LOGIN));
}
- public function showLogin():void
+ private function onLogoutClick():void
{
- loginVisible = true;
- wait = false;
- failed = false;
- usernameField.text = "";
- passwordField.text = "";
+ dispatchEvent(new Event(Login.LOGOUT));
}
- public function failLogin():void
+ public function reset():void
{
- failed = true;
- wait = false;
- }
-
- private function onLoginClick():void
- {
- wait = true;
- dispatchEvent(new Event(LoginExample.LOGIN));
+ usernameField.text = "";
+ passwordField.text = "";
}
]]>
</mx:Script>
- <mx:String id="username">{usernameField.text}</mx:String>
- <mx:String id="password">{passwordField.text}</mx:String>
+ <model:Login id="login">
+ <model:username>{usernameField.text}</model:username>
+ <model:password>{passwordField.text}</model:password>
+ </model:Login>
+
+
<!-- The main view: doesn't really do anything -->
<mx:Panel width="400" height="300" horizontalCenter="0" verticalCenter="0">
<mx:FormItem label="User Email:" fontSize="24">
- <mx:Label fontSize="24" text="{userEmail}"/>
+ <mx:Label fontSize="24" text="{user.email}"/>
</mx:FormItem>
<mx:ControlBar>
- <mx:Button label="Logout" click="dispatchEvent(new Event(LoginExample.LOGOUT))"/>
+ <mx:Button label="Logout" click="onLogoutClick()"/>
</mx:ControlBar>
</mx:Panel>
<!-- The login overlay -->
- <mx:Canvas backgroundAlpha=".9" backgroundColor="#000000" visible="{loginVisible}" width="100%" height="100%">
- <mx:Panel horizontalCenter="0" verticalCenter="0" enabled="{!wait}">
+ <mx:Canvas backgroundAlpha=".9" backgroundColor="#000000" visible="{nav.showLogin}" width="100%" height="100%">
+ <mx:Panel horizontalCenter="0" verticalCenter="0" enabled="{!login.waiting}">
<mx:Form>
<mx:FormItem label="Username">
<mx:TextInput id="usernameField" toolTip="username: bob"/>
@@ -71,8 +58,8 @@
</mx:FormItem>
<mx:Canvas width="100%">
- <mx:Button label="{(wait) ? 'Please wait...' : 'Login'}" click="onLoginClick()"/>
- <mx:Label fontWeight="bold" text="Please Try Again" color="#FF0000" right="0" visible="{failed}"/>
+ <mx:Button label="{(login.waiting) ? 'Please wait...' : 'Login'}" click="onLoginClick()"/>
+ <mx:Label fontWeight="bold" text="Please Try Again" color="#FF0000" right="0" visible="{login.failed}"/>
</mx:Canvas>
</mx:Form>
</mx:Panel>
Please sign in to comment.
Something went wrong with that request. Please try again.