While having time this weekend to review a few of the Flash projects I’ve been working on, I became aware of the idea that every software “action” has the anticipated state of failing. Login will fail if the user enters incorrect credentials or leaves a field empty; A user cancels a file copy operation; A reg form may fail if the user is not permitted to enter the site based off their information. It’s not that the application is failing because it can’t reach a web service or something unexpected happened, but rather a planned reaction was developed to give the user a negitive response to their action. In light of this, I have updated FlashMVC to allow actions to designate that it’s result is negitive/failed. Then on the SuperModel side, the view can optionally supply a fail callback function for when the action designates that it failed.

//  Action class
public class Login {
    public function Login(helper:ActionHelper, name:String, password:String) {
        if(name=="user" && password=="pw") {
            SuperSimpleApp.getInstance().userFirstName = "John Doe";
            helper.complete();
        } else {
            if(name=="" || password=="") helper.result = SuperSimpleApp.RESULT_EMPTY_FIELDS;
            helper.complete(false); // "false" tells the caller that this was not successful
        }
    }
}

The line “helper.complete(false)” tells the SuperModel that the action completed but it was a negative response. In this example, it was failing the login action. So we have the action created, now we need to create the SuperModel that its attached to and the SuperModel will also service as its model.

// SuperModel
public class SuperSimpleApp extends SuperModel {
    public static const LOGIN:String = "Login"; // String reference to the class name for the view to call
    // Flag a response to use in special situations where the view needs to know more about the result
    public static const RESULT_EMPTY_FIELDS:String = "emptyFields";
   
    public var userFirstName:String;
    public function SuperSimpleApp() {
        // Register the class to the SuperModel for use.
        // Note: once the action is added, use the string name of the class to reference it
        actionAdd(Login);
    }
    public static const getInstance():SuperSimpleApp = new SuperSimpleApp();
}

Now in Flash or in your Flex MXML you can run your Login action like this:

var superModel:SuperSimpleApp = SuperSimpleApp.getInstance();
// loginAction will hold the action class we want to call and setup the onSuccess and onFail callbacks.
var loginAction:Performance = new Performance(SuperSimpleApp.LOGIN, onSuccess, onFail)
// ...
// Now later in the code, run the action now on the SuperModel with a test username and password
function onSubmitButtonClick(e:Event) {
    superModel.process(loginAction, "user", "pw");
}
// If the user is logged in successfully
private function onSuccess(result:*):void {
    trace("Hello "+ superModel.userFirstName);
}
// If the login action designated a negative response to the request:
private function onFail(result:*):void {
    // Check the result against known responses flagged in the super model... otherwise give generic message.
    if(result==SuperSimpleApp.RESULT_EMPTY_FIELDS) trace("Please fill out all fields");
    else trace("Invalid username or password");
}

This should help having to deal with common success and fail responses from actions. Let me know what your thoughts are!


Author: Jonathan Dunlap
Jonathan is a veteran software architect, author of IsoHill, humanitarian, and has worked with Bigpoint Inc, CrowdStar, ePrize, and Microsoft.

Tags: ,