Realtime hit counterweb stats

Archive for the 'Utilities' Category

Data Binding with AS3 Signals

Monday, March 22nd, 2010

Over the past few years the Presentation Model Pattern has picked up a lot of traction in the Flex world as it allows for an abstracted representation of a views model and state which can easily be tested. Core to it’s implementation is the facilitation of data bindings between a PMs model and a corresponding View’s controls. Implementing data bindings between a view and its associated PM is quite straightforward when leveraging data binding in Flex – simply annotate a property, getter or class with [Bindable] metadata. A basic example of which can be seen as follows:

public class UserInfoPresentationModel extends EventDispatcher
{
     private var _user:User = null;

     [Bindable(event="userChangeEvent")]
     public function get country() : String
     {
          return _user.location.country;
     }

     private function updateUser(value:User) : void
     {
          _user = value;
          dispatchEvent(new Event("userChangeEvent"));
     }
     // ….
}

Then a typical Flex data binding expression can be defined within a particular property of a control where the binding is to take place against the property of the Presentation Model instance:

<!– Define the view bindings to the Presentation Model –>
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx   = "http://ns.adobe.com/mxml/2009"
         xmlns:s    = "library://ns.adobe.com/flex/spark"
         initialize = "init();" >
  <fx:Script>
    <![CDATA[
        // Define the views presentation model instance
        [Bindable]
        private var _pm:UserInfoPresentationModel = null;

        privatefunction init() : void
        {
            _pm = new UserInfoPresentationModel();
        }
    ]]>
  </fx:Script>
  <s:Label text="{ _pm.country}" />
</s:Group>

As you can see this is quite simple and easy to implement. But how does one accomplish this without the use of Flex’s native data binding facility?

This may seem like a rather moot question as its hard to imagine why someone would choose to not take advantage of Flex data binding to accomplish synchronizing a view with its PM. However, just recently I found myself in need of a solution for this problem…

I have been experimenting with AS3 Signals lately as I find them to be a nice alternative to Flash Player Events. This especially makes sense in the context of Presentation Models as, at least in my experience, event Bubbling within the display list simply isn’t necessary when binding a component to a property of a PM. Furthermore, while I am not particularly biased against Flash Players event model, its implementation is very much a black-box, and AS3 Signals allows for a good level of abstraction and control of the Signaling mechanism. So I contemplated how this may improve a Presentation Models implementation and decided to see how Signals could be implemented with a PM; however I first needed to find a solution which would allow Signals to provide the same functionality as data binding.

Essentially, implementing “pseudo” data bindings with AS3 Signals can be accomplished much the same as can be seen in BindingUtils. I developed a SignalDataBinding API which will feel somewhat familiar to those who have worked with BindingUtils in the past. SignalDataBinding provides an API allowing for the creation of pseudo data bindings within a Signal against a property of a host object which is facilitated via the creation of runtime functions which are managed by the API.

For example, suppose you are using AS3Signals and wanted to bind a label’s text property to the value of another object. With the SignalDataBinding API this could be accomplished as follows:

// Define the object which utilizes the Signal messaging API
public class TitleModel
{
    private var _bindings:SignalDataBinding = null;
    private var _titleChanged:Signal      = null;
    private var _title:String             = null;
   
    public function get title() : String
    {
        return _title;
    }
   
    public function set title(value:String) : void
    {
        _title = value;
        _titleChanged.dispatch( value );
    }
       
    // Model class provides an API for property specific
    // bindings while not exposing the underlying binding
    // mechanism, thus  allowing the data binding facility
    // to be changed transparent to that of a client’s
    // implementation.
    public function addTitleBinding(host:*, property:*):void
    {
        _bindings.addBindableListener(_titleChanged, host, property);
    }
}

Then, in the view from which the bindings are to be defined you would explicitly add bindings as follows:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx   = "http://ns.adobe.com/mxml/2009"
         xmlns:s    = "library://ns.adobe.com/flex/spark"
         initialize = "init();" >
  <fx:Script>
    <![CDATA[
        // Define the views presentation model instance
        private var _pm:TitleModel = null;
   
        private function init() : void
        {
          _pm = new TitleModel();

          // Add binding between the Model’s ‘title’
          // property and the views ‘titleLabel.text’
          // property. Assignments to "title" will now
          // be reflected in ‘titleLabel.text’
          _pm.addTitleBinding( titleLabel, "text" );
       }
       ]]>
  </fx:Script>
  <s:Label id="titleLabel" />
</s:Group>
 

And that’s basically it. Additional levels of abstraction could easily be defined in order to provide common base classes which encapsulate the SignalDataBinding instance etc. Additionally, I do not see any reason why the SignalDataBinding API could not be utilized in AS3 projects as well as Flash projects; for the underlying implementation has no dependencies on the Flex Framework. Thus the SignalDataBinding API could be leveraged in any AS3 project as is, or adapted to implement the Flash Player Event model to provide a BindingUtils implementation for AS3 and / or Flash Projects.

You can view a basic example as well as download the SignalDataBinding API source, docs and binary.

SSH / Telnet client for Blackberry

Tuesday, December 15th, 2009

I always thought it would be useful to be able to log in to my server from my phone; a BlackBerry Curve 8310. So the other day I did a quick search and found MidpSSH – an SSH1/2 and Telnet client (with VT320 terminal emulation) for MIDP 1.0 / 2.0 (J2ME) devices.

Since the Blackberry OS provides MIDP 1.0 support and OS 4 provides a subset of MIDP 2.0 support, MidpSSH seemed like it would be a good fit.

Installing MidpSSH is very straightforward – like most installers for Blackberry. UX is straightforward as well, and once installed you can simply start up the client, create a new session (which can be saved, imported etc.), enter the host name and log in. You’ll also probably want to increase the font size as the default is set to “Tiny”, which is a very small font size. I changed this setting to LCD 5×9 which works well.

All in all a simple, yet very useful free application.

Simple RPC Instrumentation in Flex

Sunday, September 20th, 2009

On occasion developers may find a need to quickly measure the time it takes for a request to a remote service to return a response back to the client without the need to employ an automated testing tool to perform the instrumentation. This information can prove quite valuable for performing application diagnostics on the client and, when measured in terms of code execution, monitoring at the execution level will always be a bit more precise than that which can be measured by using a Network proxy alone, such as Charles or Fiddler, etc.

Obviously there are numerous solutions which can be implemented to monitor the elapsed time of a service invocation, however it was my goal to provide a unified solution which could easily be implemented into existing client code without significant refactorings being required.

In order to achieve this I first needed to consider what the typical implementation of a service invocation is in order to isolate the
commonality. From there it is only a matter of determining a solution that meets the objective in the most non intrusive manner possible.

To begin let us consider what a “typical” service invocation might look like for the three most common services available in the Flex Framework; HTTPService, RemoteObject and WebService.

// HTTPService
var call:AsyncToken = service.send();
call.addResponder( this );

// RemoteObject
var call:AsyncToken = service.someMethod();
call.addResponder( this );

// WebService
var call:AsyncToken = service.someOperation();
call.addResponder( this );
 

Based on the 3 above implementations we can deduce that the common API used when performing a service invocation is AsyncToken. So to provide a unified solution for all three common Services we could either extend AsyncToken or provider an API which wraps AsyncToken. For my needs I chose to implement an API which simply monitors an AsyncToken from which the duration of an invocation can be determined, thus I wrote an RPCDiagnostics API which can be “plugged” into an AsyncToken client implementation.

RPCDiagnostics provides basic performance analysis of a Remote Procedure Call by providing a message which displays information about the operation duration via a standard trace call. In addition, an event listener of type RPCDiagnosticsEvent can be added to facilitate custom diagnostics and Logging.

RPCDiagnostics can easily be implemented as an addition to an existing AsyncToken or in place of an AsyncToken. The following examples demonstrate both implementations.

Implementing RPCDiagnostics onto an existing AsyncToken:

var call:AsyncToken = null;
call = RPCDiagnostics.monitorToken(service.send(),"methodName");
call.addResponder();
 

Implementing RPCDiagnostics in place of an AsyncToken:

var call:RPCDiagnostics = null;
call = new RPCDiagnostics( service.send(), "methodName" );
call.addResponder();
 

Implementing a listener to an RPCDiagnostics instance:

var call:RPCDiagnostics = null;
call = new RPCDiagnostics( service.send(), "operationName" );
call.addResponder();
call.addEventListener( RPCDiagnosticsEvent.EXECUTION_COMPLETE,
                       handler);
 

The RPCDiagnostics API and dependencies can be downloaded via the Open Source AS3 APIs page or from the below links:

RPCDiagnostics
RPCDiagnosticsEvent
Execution

Open Source AS3 APIs

Sunday, August 30th, 2009

For the past 4 years or so I have provided quite a few AS3 APIs as Open Source to the Flex Community, via my blog. These APIs can typically be found at the Open Source AS3 APIs page however, the page is basically just a URI to a series of arbitrarily added AS3 source classes. It was originally intended to simply serve as a convenient location to access the source, and it had always been my intention to eventually break out all of these APIs into seperate SVN projects.

So with that being said I am finally in the process of making the structural changes I had originally envisioned. Moving forward I will begin the process of creating seperate SVN projects for these Open Source APIs; with the primary goal being to provide practical APIs that only require minimal (if any) dependencies on additional libraries, complete test coverage via Flex Unit 4 and Mavenized builds.

The first project to move over to the new project structure will be the AS3 collections project as the classes in this package, specifically HashMap, have proven to provide the most value according to community feedback.

So stayed tuned!