Flash Forward

Yesterday was probably the first official day of working with Flash and actually producing something worthwhile (rather than a tutorial). I have found that there may be a way to make everything flow together a little easier.  Coldfusion in the last 3 versions now (we’re on 8, the current version is 9, the development started in 7) had put together a cool idea using CFCs.  Basically, we can use CFC as an encapsulation of what most programmers would consider an object in object oriented design.  Within that object, you can call functions so that you can keep the design and business logic separate (framework anyone?) which solves our overhead problem quite easily.  What does that mean then?  We are able to design one piece of logic that can be used either by coldfusion web pages or jQuery or Flash.  This makes development quite easy for everyone involved in the projects.  This also means that we can actually have one developer working on the ‘business logic’ and one working on the interface and its connection to that code.  It’s pretty slick.  So here’s how I went about it, as an example.

I built a login form, it contained a username field and a password field with of course a submit button.  Since we’re working with AS3 and CS3, we use the flash.net NetConnection and Responder functionality.  This particular example does not account for session variables, I’m still working on that idea, but it does show a call to a cfc function with named parameters that tests whether or not the login is valid or not and sends those results back.

------------AS3 Code------------
import flash.net.*;
var userService:NetConnection = new NetConnection();
//userService.objectEncoding = ObjectEncoding.AMF3;
userService.objectEncoding = ObjectEncoding.AMF0; //complex types cannot be sent via remote calls (struct args) unless set to AMF0
flash.system.Security.allowDomain("*"); //allow cross scripting, or so they say
userService.connect(http://subdomain.ddamedical.com/flashservices/gateway); //connect to the flash gateway provided by Coldfusion

submit_btn.addEventListener(MouseEvent.CLICK,ValidateAndSend); //add listener function to submit button

//create the responder for the call results
var responder:Responder = new Responder(getLoginResults,onSubmissionError);

//function for error on submission, returns object
function onSubmissionError(err:Object):void{
trace(“There was a problem: “);
showErrorObject(err);
}

//display error in trace given the returned ‘error’ object
function showErrorObject(err:Object):void{
trace(“Description: ” + err.description);
trace(“Type: ” + err.type);
trace(“Details: “+ err.details);
trace(“Level: “+ err.level);
trace(“Code: ” + err.code);
}

//parse the results that are sent back from the function call
function getLoginResults(resultText:Object):void{
//returning struct from CF with ISVALIDUSER=true/false and ERRMESSAGE=description of the error or verification message – return always in full upper case
for (var prop in resultText){trace(prop)};
login_results.text = resultText.ERRMESSAGE;
if(resultText.ISVALIDUSER==”true”){
trace(“valid user”);
//do something here to move the user onward. Set variables???
}

}

//function to validate and send form
function ValidateAndSend(Event:MouseEvent):void{
//validate fields
login_results.text=”";
if(!username_txt.length){
login_results.appendText(“Please enter your username.\n”);
}
if (!password_txt.length){
login_results.appendText(“Please enter your password.\n”);
}
if (!login_results.length){
// form fields are ok, send data to cf
login_results.text=”Sending….”;
//create a parameters object to send to Coldfusion – parameters with names are easier to work with in CF as named arguments to the function
var parameters:Object = new Object();
parameters.loginname=username_txt.text;
parameters.passcode=password_txt.text;
userService.call(“cfc.user.remoteLogin”,responder,parameters);

}//end validate fields
}//end validate and send

————CF Function Code————
<cffunction  name=”remoteLogin”  returntype=”struct”  output=”false” access=”remote” description=”give ability to remotely login from flash or jquery”>
<cfargument name=”loginname”  type=”string”    required=”no” default=”">
<cfargument name=”passCode”  type=”string”    required=”no” default=”">

<cfset var isOK = false>

<cfset var returnStruct = StructNew()>
<cfset returnStruct.isValidUser = false>
<cfset returnStruct.errMessage = “”>

<cfif arguments.loginname eq “”>
<cfset returnStruct.errMessage = “Username has not been entered and is required for login.#chr(10)#”>
</cfif>
<cfif arguments.passCode eq “”>
<cfset returnStruct.errMessage = “#returnStruct.errMessage#Password has not been entered and is required for login.#chr(10)#”>
</cfif>
<cfset isOK = isValidUser(loginname=arguments.loginname,passcode=arguments.passcode)>

<cfif isOK>
<cfset returnStruct.errMessage =  “Login verified.” />
<cfset returnStruct.isValidUser = true>
<cfelse>
<cfset returnStruct.errMessage =   “Login credentials do not match. Please try again.” />
</cfif>
<cfreturn returnStruct>
</cffunction>