Permalink
Browse files

fixed up my approach. .. removed old respond tags.. doing it in as

  • Loading branch information...
1 parent ed4284e commit 06d8a2afb86a0294cc6f67cce8eb254a0cef9cde @seanhess committed Apr 30, 2009
@@ -0,0 +1,70 @@
+package login.control
+{
+ import flash.events.Event;
+
+ import login.model.Application;
+ import login.model.Login;
+ import login.model.Navigation;
+ import login.model.User;
+ import login.service.ParseAuthResponses;
+
+ import mx.rpc.events.ResultEvent;
+ import mx.rpc.http.HTTPService;
+
+ import net.seanhess.glue.Respond;
+
+ public class Authentication
+ {
+ public var auth:Authentication;
+ public var nav:Navigation;
+ public var app:Application;
+
+ public var parse:ParseAuthResponses;
+
+ public var loginService:HTTPService;
+ public var logoutService:HTTPService;
+
+ public function login(attempt:Login):void
+ {
+ var respond:Respond = new Respond(loginService, attempt);
+ respond.result(onLogin);
+
+ attempt.waiting = true;
+
+ loginService.send({username:attempt.username, password:attempt.password});
+ }
+
+ public function logout():void
+ {
+ var respond:Respond = new Respond(logoutService);
+ respond.result(onLogout);
+
+ logoutService.send();
+ }
+
+ private function onLogin(event:ResultEvent, respond:Respond):void
+ {
+ var attempt:Login = respond.data as Login;
+ attempt.waiting = false;
+
+ var user:User = parse.parseLoginResponse(event.result);
+
+ if (user.valid)
+ {
+ app.currentUser = user;
+ nav.loggedIn();
+ attempt.reset();
+ }
+ else
+ {
+ attempt.failed = true;
+ }
+ }
+
+ private function onLogout(event:Event):void
+ {
+ app.currentUser = null;
+ nav.loggedOut();
+ }
+ }
+}
@@ -1,73 +0,0 @@
-<?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.*" xmlns="http://glue.seanhess.net/2009">
-
- <!--
- This controller is responsible for invoking services and
- manipulating models.
- -->
-
- <!-- Models: These are injected -->
- <model:Application id="app"/>
- <model:Navigation id="nav"/>
-
- <!-- Services: Either injected or created. -->
- <service:ParseAuthResponses id="parser"/>
-
- <!-- These tags make it so we can listen to any service that is injected with minimal code -->
- <Respond id="respondLogin" result="onLogin(event.source)"/>
- <Respond id="respondLogout" result="onLogout(event.source)"/>
-
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.rpc.http.HTTPService;
- import login.model.Login;
- import login.model.User;
-
- // These are injected in from the glue
- [Bindable] public var loginService:HTTPService;
- [Bindable] public var logoutService:HTTPService;
-
- private var currentAttempt:Login;
-
- public function login(value:Login):void
- {
- currentAttempt = value;
- currentAttempt.start();
- respondLogin.start(loginService);
- loginService.send({username:currentAttempt.username, password:currentAttempt.password});
- }
-
- public function logout():void
- {
- respondLogout.start(logoutService);
- logoutService.send();
- }
-
- 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>
@@ -13,6 +13,7 @@
<service:MockLoginService id="mockLogin"/>
<service:DoNothingService id="doNothing"/>
+ <service:ParseAuthResponses id="authParser"/>
<!-- MODELS -->
@@ -21,7 +22,7 @@
<!-- CONTROLLERS -->
- <control:Authentication id="auth" app="{app}" nav="{nav}" loginService="{mockLogin}" logoutService="{doNothing}"/>
+ <control:Authentication id="auth" app="{app}" nav="{nav}" loginService="{login}" logoutService="{logout}" parse="{authParser}"/>
<Glue>
<view:MainView id="mainView"/>
@@ -1,11 +1,14 @@
package login.model
{
+ import flash.events.Event;
+
[Bindable]
+ [Event(name="reset", type="flash.events.Event")]
public class Login
{
- public static const LOGIN:String = "login";
- public static const LOGOUT:String = "logout";
- public static const FAIL:String = "fail";
+ public static const LOGIN:String = "login";
+ public static const LOGOUT:String = "logout";
+ public static const RESET:String = "reset";
public var username:String;
public var password:String;
@@ -19,6 +22,7 @@ package login.model
password = "";
waiting = false;
failed = false;
+ dispatchEvent(new Event(RESET));
}
public function start():void
@@ -27,7 +27,7 @@
]]>
</mx:Script>
- <model:Login id="login">
+ <model:Login id="login" reset="reset()">
<model:username>{usernameField.text}</model:username>
<model:password>{passwordField.text}</model:password>
</model:Login>
@@ -50,11 +50,11 @@
<mx:Panel horizontalCenter="0" verticalCenter="0" enabled="{!login.waiting}">
<mx:Form>
<mx:FormItem label="Username">
- <mx:TextInput id="usernameField" toolTip="username: bob"/>
+ <mx:TextInput id="usernameField" toolTip="username: bob" enter="onLoginClick()"/>
</mx:FormItem>
<mx:FormItem label="Password">
- <mx:TextInput id="passwordField" displayAsPassword="true" toolTip="password: bob"/>
+ <mx:TextInput id="passwordField" displayAsPassword="true" toolTip="password: bob" enter="onLoginClick()"/>
</mx:FormItem>
<mx:Canvas width="100%">
View
@@ -5,7 +5,6 @@
<component class="net.seanhess.glue.IGlueAction"/>
<component class="net.seanhess.glue.Observe"/>
<component class="net.seanhess.glue.Respond"/>
- <component class="net.seanhess.glue.RespondEvent"/>
<component class="net.seanhess.glue.Smart"/>
<component class="net.seanhess.glue.GlueMap"/>
<component class="net.seanhess.glue.Route"/>
@@ -64,6 +64,16 @@ package net.seanhess.glue
_on = value;
}
+ public function set model(value:IEventDispatcher):void
+ {
+ _on = value;
+ }
+
+ public function set service(value:IEventDispatcher):void
+ {
+ _on = value;
+ }
+
public function get on():IEventDispatcher
{
return _on;
@@ -1,49 +1,91 @@
package net.seanhess.glue
{
import flash.events.Event;
- import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
+ import flash.utils.Dictionary;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
/**
+ * Figure out how to get it to use a responder too.
+ *
+ * I need to figure out how to clean this up...
+ *
+ *
*/
- [Event(name="result",type="net.seanhess.glue.RespondEvent")]
- [Event(name="fault", type="net.seanhess.glue.RespondEvent")]
- public class Respond extends EventDispatcher
+ public class Respond
{
- public var resultEvent:String = ResultEvent.RESULT;
- public var faultEvent:String = FaultEvent.FAULT;
- public var autoFinish:Boolean = true;
+ public var service:IEventDispatcher;
+ public var data:*;
+ public var handlers:Dictionary;
+ public var autoClean:Boolean = true;
- public var instance:IEventDispatcher;
+ public function Respond(service:IEventDispatcher, data:*=null, autoClean:Boolean = true)
+ {
+ this.service = service;
+ this.data = data;
+ this.autoClean = autoClean;
+ handlers = new Dictionary(true);
+ }
- public function start(instance:IEventDispatcher):void
+ public function listen(type:String, handler:Function):void
{
- this.instance = instance;
- instance.addEventListener(resultEvent, onResult);
- instance.addEventListener(faultEvent, onFault);
+ service.addEventListener(type, callback);
+ handlers[type] = handler;
}
- public function finish():void
+ public function result(handler:Function):void
{
- instance.removeEventListener(resultEvent, onResult);
- instance.removeEventListener(faultEvent, onFault);
+ listen(ResultEvent.RESULT, handler);
}
- private function onResult(event:Event):void
+ public function fault(handler:Function):void
{
- if (autoFinish) finish();
- dispatchEvent(new RespondEvent(RespondEvent.RESULT, event));
+ listen(FaultEvent.FAULT, handler);
}
- private function onFault(event:Event):void
+ protected function callback(event:Event):void
{
- if (autoFinish) finish();
- dispatchEvent(new RespondEvent(RespondEvent.FAULT, event));
+ var handler:Function = handlers[event.type];
+ remove(event.type);
+
+ try
+ {
+ handler(event, this);
+ }
+
+ catch (mainError:ArgumentError)
+ {
+ try
+ {
+ handler(event);
+ }
+ catch (subError:ArgumentError)
+ {
+ throw new Error("Could not determine how to call handler for event " + event.type);
+ }
+ }
+
+ if (autoClean)
+ clean();
}
+ public function clean():void
+ {
+ for (var type:String in handlers)
+ remove(type);
+ }
+
+ public function remove(type:String):void
+ {
+ var handler:Function = handlers[type];
+
+ if (handler != null)
+ service.removeEventListener(type, callback);
+
+ delete handlers[type];
+ }
}
}
@@ -1,21 +0,0 @@
-package net.seanhess.glue
-{
- import flash.events.Event;
-
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
-
- public class RespondEvent extends Event
- {
- public static const RESULT:String = ResultEvent.RESULT;
- public static const FAULT:String = FaultEvent.FAULT;
-
- public var source:*;
-
- public function RespondEvent(type:String, source:*):void
- {
- this.source = source;
- super(type);
- }
- }
-}

0 comments on commit 06d8a2a

Please sign in to comment.