Programming, Visualization and Game Development Theory

07Apr FlashMVC Update: addResultListeners

flashmvc

I have just finished up making another addition to my framework FlashMVC. The update effectively removes the need to use ActionBinder as I will indicate below. ActionBinder was a neat utility, but ultimately I wanted solution that was just as simplistic but could be usable for both Flash and Flex. I couldn’t find any easy solutions until I thought about retrofitting addEventListener with an alternate form to allow callback functions to fire for specific commands on the SuperModel. Hence, I have added these three new methods to SuperModel:

.addResultListeners(actionName:String, onSuccess:Function, onFail:Function)
.addStatusListeners(actionName:String, enabled:Function, disabled:Function)
.addHasActionListeners(actionName:String, added:Function, removed:Function)

Now in your view, you can do something like this in your view:

var app:MySuperApplication = MySuperApplication.getInstance();
app.addResultListeners(MySuperApplication.Login, onSuccess, onFail);
// SuperModel.perform(name of the action, optional onComplete function, ...Rest for the parameters of the action class)
app.perform(MySuperApplication.Login, null, "test@invalidemail@.com");

function onSuccess(result:*):void {
   trace("Login was successful");
}
function onFail(result:*):void {
   if(result==MySuperApplication.RESULT_INVALID_EMAIL)
         trace("invalid email used");
}

Updated ASDocs Here

Aside from this addition, I added in some safeguards like throwing an error if a command tries to call ActionHelper.complete() more than once as well as some other fixes. ActionBinder is still useful in the Flex world as it can help increase readability in some cases, but SuperModel now has most of the nice features built into it.

Please let me know if you have any comments! I am still working on adding in the replay feature to the framework which hopefully I will have done soon.

Tags:

21Feb FlashMVC API Update

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!

Tags: ,

09Dec flashMVC: New Utility ActionBinder!

I just update the SVN (svn.jadbox.com/public/) source for flashMVC with a MXML component tool I made for the cases that I was working with Flex. It’s located in com/jadbox/flashMVC/flex/ActionBinder.as, and it helps automate the view (aka MXML) portion of your application to communicate with your SuperModel. It will monitor the status of a particular action on the model, and allow the user to easily perform view updates or to run the action itself. This will help further reduce the amount of work you will have to do in Script tags (if not completely remove the need for one in the MXML).

// Example
<flashMVC:ActionBinder superModel="{mySuperModel}"
    action="{mySuperModel.LOGIN}"    
    id="LoginBinder"     
    actionComplete="{trace('Login action completed')}"   
    actionEnable="{trace('action enabled')}"     
    actionDisable="{trace('action disabled')}"   
    actionPerforming="{trace('working')}"    
    actionStoppedPerforming="{trace('stopped working')}" />

// You can also do the action from the wrapper:
myActionBinder.perform(...args);
LoginBinder.perform(username.text, password.test); // for example

// Can use this property to
// enable or disable the action as well:
myActionBinder.actionEnabled=false;

You can find a great example of this in my SVN under the path:
/flex projects/flashMVC examples/actionBinderExample/
You will just be sure to link the flashMVC framework from the Flex project settings to compile.

Asdocs have been updated with the ActionBinder’s API. For more information about nimble flashMVC framework, go the flashMVC tab at the top.

Next on my list of features for flashMVC is the application “replayability from imported xml log”. This will be really useful to utilize in Flash or Flex development!

Tags: ,

12Sep flashMVC ASDocs

I just wanted to make a quick update on flashMVC as I have made some further refinements and added ASDocs. Again, please let me know if you have any suggestions or comments!

You can access the docs here: flashMVC ASDocs

Tags: ,

10Sep flashMVC beta released

I have finally released a pet-project of mine for handling simple to complex Flash and Flex applications in a unified way. The framework provides a pattern for MVC (Model View Controller) and has lots of goodies to rapidly build prototypes. In the near future, I will be working on an extension to allow quick unit testing as well as better error handling. This is new so please leave feedback and comments! (-_^)P

flashMVC information and download page

Tags: ,