Flash Login Tutorial
Here is a simple Flash application that simulates logging into a website with an email address.
Download the example Flash 9 source code.
Flash AS3 – Frame 1 timeline code for the view:
“email” is a TextInput component
“submitButton” is a MovieClip that acts as a submit button
“status” is a label field
import com.jadbox.flashmvc2.SuperEvent;
SuperWebsite.gi.actionLogin.addEventListener(SuperEvent.COMPLETE, onLogin);
submitButton.addEventListener(MouseEvent.CLICK, clickLogin);
function clickLogin(e:Event):void {
submitButton.enabled=false;
SuperWebsite.gi.runEvent(new LoginEvent(email.text));
}
function onLogin(e:LoginEvent):void {
submitButton.enabled=true;
if(e.successful) {
trace("Success on login for email "+ e.email);
status.text = "Welcome " + SuperWebsite.gi.user_profile;
}
if(e.errorWasBlank) status.text = "email was blank";
else if(e.errorWasInvalid) status.text = "email was invalid";
}
SuperWebsite.gi.actionLogin.addEventListener(SuperEvent.COMPLETE, onLogin);
submitButton.addEventListener(MouseEvent.CLICK, clickLogin);
function clickLogin(e:Event):void {
submitButton.enabled=false;
SuperWebsite.gi.runEvent(new LoginEvent(email.text));
}
function onLogin(e:LoginEvent):void {
submitButton.enabled=true;
if(e.successful) {
trace("Success on login for email "+ e.email);
status.text = "Welcome " + SuperWebsite.gi.user_profile;
}
if(e.errorWasBlank) status.text = "email was blank";
else if(e.errorWasInvalid) status.text = "email was invalid";
}
SuperWebsite (The SuperModel)
package
{
import com.jadbox.flashmvc2.SuperAction;
import com.jadbox.flashmvc2.SuperModel;
public class SuperWebsite extends SuperModel
{
// SuperWebsite.gi.something is my shorthand for usual Singleton.getInstance().something
// However, the SupelModel does not have to be a singleton and could be a property of your base Application.
public static const gi:SuperWebsite = new SuperWebsite();
public var user_profile:String;
// Add other properties for variables that are shared among actions or, if needed, views.
public function SuperWebsite()
{
super();
}
public var actionLogin:SuperAction = new SuperAction(LoginAction, LoginEvent);
// Add additional SuperAction properties here to link to actions and their event
}
}
{
import com.jadbox.flashmvc2.SuperAction;
import com.jadbox.flashmvc2.SuperModel;
public class SuperWebsite extends SuperModel
{
// SuperWebsite.gi.something is my shorthand for usual Singleton.getInstance().something
// However, the SupelModel does not have to be a singleton and could be a property of your base Application.
public static const gi:SuperWebsite = new SuperWebsite();
public var user_profile:String;
// Add other properties for variables that are shared among actions or, if needed, views.
public function SuperWebsite()
{
super();
}
public var actionLogin:SuperAction = new SuperAction(LoginAction, LoginEvent);
// Add additional SuperAction properties here to link to actions and their event
}
}
LoginEvent (SuperEvent): Used a model for LoginAction
package
{
import com.jadbox.flashmvc2.SuperEvent;
public class LoginEvent extends SuperEvent
{
public var errorWasBlank:Boolean;
public var errorWasInvalid:Boolean;
public var email:String;
public function LoginEvent(email:String)
{
super(email);
}
}
}
{
import com.jadbox.flashmvc2.SuperEvent;
public class LoginEvent extends SuperEvent
{
public var errorWasBlank:Boolean;
public var errorWasInvalid:Boolean;
public var email:String;
public function LoginEvent(email:String)
{
super(email);
}
}
}
LoginAction – Note that this class does not need to extend/implement any other classes. It just needs it’s first parameter to be a SuperEvent type for reference.
package
{
import com.jadbox.flashmvc2.SuperEvent;
import com.jadbox.flashmvc2.SuperModel;
import flash.events.Event;
import flash.utils.*;
public class LoginAction {
public var superWebsite:SuperWebsite;
private var event:LoginEvent;
public function LoginAction(event:LoginEvent, email:String)
{
trace("action class email "+email);
this.event = event;
superWebsite = event._superModel as SuperWebsite;
event.errorWasBlank = !email || email=="";
event.errorWasInvalid = email.indexOf("@") == -1 || email.indexOf(".") == -1;
event.email = email;
superWebsite.user_profile = "";
var noError:Boolean = !event.errorWasBlank && !event.errorWasInvalid;
if(noError) superWebsite.user_profile = "John Doe";
event.complete(noError);
}
}
}
{
import com.jadbox.flashmvc2.SuperEvent;
import com.jadbox.flashmvc2.SuperModel;
import flash.events.Event;
import flash.utils.*;
public class LoginAction {
public var superWebsite:SuperWebsite;
private var event:LoginEvent;
public function LoginAction(event:LoginEvent, email:String)
{
trace("action class email "+email);
this.event = event;
superWebsite = event._superModel as SuperWebsite;
event.errorWasBlank = !email || email=="";
event.errorWasInvalid = email.indexOf("@") == -1 || email.indexOf(".") == -1;
event.email = email;
superWebsite.user_profile = "";
var noError:Boolean = !event.errorWasBlank && !event.errorWasInvalid;
if(noError) superWebsite.user_profile = "John Doe";
event.complete(noError);
}
}
}