Kilometer0

SWFAddress + AS3Signals + RobotLegs Demo « Km0 Blog

SWFAddress + AS3Signals + RobotLegs Demo
- by admin

As a follow up to my previous post on an SWFAddress helper class that uses AS3Signals instead of events, I created a demo application that combines SWFAddress, AS3Signals, and RobotLegs.  I used this structure on an app I currently built successfully and would use it again.  Comments and suggestions are encouraged.

The key pieces to this application are: SWFAddress helper class that uses AS3Signals, the application model, the view state model, a command to pass the application model parameters to SWFAddress, and a command to process SWFAddress changes and update the application model.

SWFAddress

You can grab the updated SWFAddress file here.  Right now it’s in the com.kilometer0 package, but you can move it to whatever package you want.  It’s not dependent on any files.

The relevant signals are:

/**
  * Dispatched when SWFAddress initializes.
  */
public var init:Signal;

/**
  * Dispatched when any value change.
  */
public var change:Signal;

/**
  * Dispatched when value was changed by Flash.
  */
public var internalChange:Signal;

/**
  * Dispatched when value was changed by Browser.
  */
public var externalChange:Signal;

Application Model

The application model class holds the variables that your application uses to build it’s state.  In my demo, I modify the ApplicationModel class whenever a user has performed an action that needs to update the state of the application.

public class ApplicationModel extends Actor
{
	public var option1:String;
	public var option2:String;
}

View State Model

I used the View State Model to hold values related to building views. In my demo, this is a super simple class that just has the current view name.  When the view name is changed, a ViewStateChanged signal is dispatched. This signal is a singleton managed by RobotLegs and is injected into the class when it’s instantiated.  This signal is handled by the MainUIMediator in the demo.  The mediator then calls the current view and tells it to hide, then creates the new view if it doesn’t exist already, then shows the new view.

public class ViewStateModel extends Actor
{
	[Inject]
	public var viewStateChanged:ViewStateChanged;

	protected var _viewName:String;

	public function get viewName():String { return _viewName; };
	public function set viewName(val:String):void
	{
		var changed:Boolean = val != _viewName;
		_viewName = val;
		if(changed)
			viewStateChanged.dispatch();
	}
}

The Change Application State Command

This command is wired to the ModifyApplicationState signal. When the signal is dispatched, this command gets executed. It takes the parameters from the ViewStateModel and ApplicationModel then converts them into a URL that is passed to SWFAddress. SWFAddress then modifies the browser address bar, which SWFAddress captures and in turn dispatches a signal letting the application know that the state has been modified.

public class ChangeApplicationStateCommand extends SignalCommand
{
	[Inject]
	public var appModel:ApplicationModel;

	[Inject]
	public var viewState:ViewStateModel;

	[Inject]
	public var swfAddress:SWFAddress;

	override public function execute():void
	{
		trace("ChangeApplicationStateCommand: execute");
		var url:String = "/"+viewState.viewName+"?";
		if(appModel.option1 != null)
			url+="op1="+appModel.option1;
		if(appModel.option2 != null)
			url+="&op2="+appModel.option2;

		swfAddress.setValue(url);

	}

}

The Application State Changed Command

This command gets fired when SWFAddress is modified. It parses the SWFAddress URL and parameters and sets them on the ApplicationModel and ViewStateModel.

public class ApplicationStateChangedCommand extends SignalCommand
{
	[Inject]
	public var viewState:ViewStateModel;

	[Inject]
	public var appModel:ApplicationModel;

	[Inject]
	public var swfAddress:SWFAddress;

	override public function execute():void
	{
		trace("ApplicationStateChangedCommand: execute");
		trace(swfAddress.getValue());

		appModel.option1 = swfAddress.getParameter("op1") as String;
		appModel.option2 = swfAddress.getParameter("op2") as String;

		viewState.viewName = swfAddress.getPath().substr(1);

	}

}

You can view the demo of the application and how it works here: /demos/sasrl/.

You can download the demo application source code here (Flex Builder AS3 Only project): SWFAddressSignalsRobotLegs.zip

share
 

Leave a comment

You must be connected to post a comment.