Archive for the 'actionscript 3' Category

Model Adapters - A binding pattern using an Adapter

Binding in Flex is great. It’s an ultra convienent way to get information from your data model to show up in your views. But it does have some limitations, and to work around those limitations I’ve been using the “Model Adapter” aka the “Wrapper” or just plain “Adapter” pattern (some info, and more).

The basic idea is you shouldn’t have to modify your data model to use it in a specific view. If you need to filter, sort, or summarize the data for a view you can do that through an Adapter so your model doesn’t need to understand that logic and you’re view isn’t reliant on a specific implementation of your model.

Example: If you had a list of books in an array, and you want to filter by some property of books (say publisher) you shouldn’t apply the filter directly to the model. Instead, create an adapter that can watch that array, and have that adapter apply the filter (or sort, or whatever).

More Examples: Consider Timeliner XE, a product I’ve been working on at my day-job. The main data model is a list of events. There are several views for that data. We have a text based / grid view, and some graphical views. Here’s a couple screenshots:

Each of those screenshots has 2 views active at a time, the grid, and then a seperate graphical view. That makes 3 views that all want to bind to our data model. But, notice the grid has 5 events in it, while the graphical views only have 3. This is because only 3 of those events are valid to plot (they have a date). It’d be nice if we only had to bind to a list of events that actually has the data we want.

Now take a look at these three screenshots from AgileAgenda, my project scheduling application.

In all of these the data we have is a list of tasks. The first two show one view with two different filters applied to the data. The third shows a large grid with all of our tasks, and a much shorter pulldown that only has the tasks that are also milestones. (A milestone is a specific type of task)

To create an adapter:

  • Create a new adapter class
  • Create a constructor for that class that takes in the “source” data model, and any options that might be specific to the adapter.
  • Add event listeners to the “source” model.
  • Write event handlers in the adapter to update the adapter’s internal state when the source changes.
  • Write accessors in the adapter, so other components can get data from it.

A simple example…

Click here to run a simple example. View-source is enabled in that. Here’s a screenshot of the example:

When you run the example, it creates a simple data model, populates that data model with 4 sample items, and then creates 4 panels. Each of those panels represents a view. The example also creates 4 different model adapters all from the same data model, but with different options set. Then each panel gets a different adapter.

As you add items to the data model, you can see that the 4 views update depending on whether or not they are filtered and sorted.

Our Data Model:

package
{
    public  DataItemExample
    {
        public var name:String;
        public var amount:Number;
        public var active:Boolean;
    }
}

—-

package
{
    import mx.collections.ArrayCollection;

    public  DataModelExample
    {
        [Bindable] public var myDataItems:ArrayCollection = new ArrayCollection();

    }
}

As you can see, it’s a pretty simple data model. There are items with a name, amount and active properties, and then there is DataModelExample class with an array of those. Notice that no view-specific data is in there.

Now, lets create our adapter and name it “AdapterExample”

First, create a constructor and some variables to hold some information about the adapter. We’ll have 2 options. onlyActiveItems and sorted. For sorted, we’ll also create a Sort object to actually do the sort for us. And we’ll also create an array to hold our filtered/sorted list of items. Note that we add an event listener for the COLLECTION_CHANGE event. This is how we’ll propogate changes from the data model to our adapter. We’ll see the handler for that later.

 public  AdapterExample extends EventDispatcher
    {
        protected var model:DataModelExample;
        protected var filteredDataItems:ArrayCollection = new ArrayCollection();
        protected var _onlyActiveItems:Boolean;
        protected var _sorted:Boolean = false;
        protected var sort:Sort;        

        public function AdapterExample(dataModel:DataModelExample, onlyActiveItems:Boolean, sorted:Boolean)
        {
            _sorted = sorted;
            _onlyActiveItems = onlyActiveItems;

            model = dataModel;
            model.myDataItems.addEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );    

            if(sorted)
            {
                sort = new Sort();
                sort.fields = [new SortField("name",true)];
            }

            rebuildFilteredArray();
        }

Notice that we called rebuildFilteredArray above. Lets write that next. All this method does is loop through our data model and grab all the items from it (respecting our filtering option) and adds them to our internal array. It also applies the sort if neccessary. At the end we dispatch two events which will be used for binding later.

  protected function rebuildFilteredArray() : void
        {
            var tmp:Array = [];
            for each ( var item:DataItemExample in model.myDataItems )
            {
                if( (! _onlyActiveItems ) || (item.active) )
                {
                    tmp.push(item);
                }
            }                        

            filteredDataItems = new ArrayCollection(tmp);

            if( sort )
            {
                filteredDataItems.sort = sort;
                filteredDataItems.refresh();
            }

            dispatchEvent(new Event(”dataItemsUpdated”) );
            dispatchEvent(new Event(”totalChanged”) );
        }

So now if we made an adapter it would start up, read in the source data model, and populate our internal array of items. But it wouldn’t respond to changes in the source data model. So lets create the event handler that we set up in the constructor. We’ll also create a couple helper methods

    protected function onItemsChanged(event:CollectionEvent):void
        {
            switch(event.kind)
            {
                case CollectionEventKind.ADD: addItems(event.items); break;
                case CollectionEventKind.REMOVE: removeItems(event.items); break;

                case CollectionEventKind.MOVE:
                case CollectionEventKind.REFRESH:
                case CollectionEventKind.REPLACE:
                case CollectionEventKind.RESET:    rebuildFilteredArray();
                                                break;

                case CollectionEventKind.UPDATE:  

            }

        }

        protected function addItems(items:Array):void
        {
            for each ( var item:DataItemExample in items )
            {
                if( (! _onlyActiveItems ) || (item.active) )
                {
                    filteredDataItems.addItem(item);
                }
            }
            dispatchEvent(new Event("totalChanged") );
        }

        protected function removeItems(items:Array):void
        {
            for each ( var item:DataItemExample in items )
            {
                var index:int = filteredDataItems.getItemIndex(item);
                if( index != -1 )
                {
                    filteredDataItems.removeItemAt(index);
                }
            }
            dispatchEvent(new Event("totalChanged") );
        }

For adding/removing items we’re going to our internal array and manually adding or removing items from it. We’re making sure to account for filtered items, but the sort object is taking care of the sorting for us.

For the other types of events, we’re kind of cheating. We only really care about adding / removing operations so we’ll just rebuild our entire internal array on other types of events. If your application uses those types of events often, you should implement them in the adapter in a more efficient manner.

Exposing data from the Adapter

We now have the internal state of the adapter updating as the model changes. So the only thing left to do in there is expose some properties so we can get at that info from our view. Let’s write two bindable getters. One of them will summarize the data (get total()) the other will give us our filtered list (get dataItems())

Note that we set the event=”" property in the [Bindable] tags so our views can correctly know when these properties change.

  [Bindable(event="dataItemsUpdated")]
        public function get dataItems() : ArrayCollection
        {
            return filteredDataItems;
        }

        [Bindable(event="totalChanged")]
        public function get total() : Number
        {
            var total:Number = 0;
            for each ( var item:DataItemExample in filteredDataItems )
            {
                total += item.amount;
            }    

            return total;
        }

Using the Adapter

Once you’ve done all of that, you can actually use your adapter. So create your data model, create your adapter, and use it!

 [Bindable] protected var dataModel:DataModelExample = new DataModelExample();
 [Bindable] protected var example1:AdapterExample = new AdapterExample( dataModel, true ,true);

    <mx:Panel x=”10” y=”218” width=”250” height=”200” layout=”absolute” title=”Filtered, Sorted>
        <mx:Label x=”10” y=”132” text=”Total:/>
        <mx:Label x=”56” y=”132” text=”{example1.total}/>
        <mx:List x=”10” y=”4” width=”210” height=”120” dataProvider=”{example1.dataItems}” labelField=”name></mx:List>
    </mx:Panel>

If you look at the source of the example, we actually create 4 adapters with varying options.

Beyond this basic example

If you want your adapter to respect changes to individual items, your items should implement the IPropertyChangeNotifier interface. So in our example if we edited an item so it’s active flag changed, the views would not update. To make that work we would implement that IPropertyChangeNotifier interface, and then write some code for the CollectionEventKind.UPDATE event.

Often times only one or two views are visible at a time and it’d be nice if all the views in the background weren’t madly updating themselves every change. To accomplish that I often write an enable() disable() method on the adapter. They usually look something like this:

protected function enable() : void

{

model.myDataItems.addEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );

rebuildFilteredArray();

}

protected function disable() : void

{

model.myDataItems.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );

}

The Essential Guide to Open Source Flash Development

The book I’ve been working on, The Essential Guide to Open Source Flash Development, is now out in stores.  It’s hard to believe that I started working on it about 11 months ago!  It’s really great to see all of that hard work finally in print.

So What is it about?

The book does a few things.  First, about a third of the book introduces you to some open source tools for doing flash development.  Things like FlashDevelop, MTASC, SwfMill, ANT, and ASDT.  It’ll show you how to create an AS2 and an AS3 based flash application using completely free and open software.  This goes all the way from installing the tools, creating a sample app, writing up some unit tests for it, and then to publishing it to the web.  Along the way it’ll give you a brief introduction to each tool, explain what it does, and then give a quick example of how to use it.  (That’s the 5 chapters I wrote)

The remaining 2/3 of the book dedicates a chapter to various open source projects going into a little more detail about them.  There’s a chapter on Papervision 3D, SWX, FUSE/Go, HAXE, AMFPHP, two for Red5 and a couple more.

This was a lot of fun to work on, and my only regret is not getting to know the other authors better.

If you’re looking for a place to buy it, check out Bookpool.   I worked for them for a year and they’re really stellar guys.  They offer good prices, but more importantly;  as long as the book is in stock, they do their damndest to get it on a truck the day you order it. (Of course, you’re at the mercy of the publisher if it’s out of stock)

Creating a simple diagramming application

Tonight, I’ll be giving a short talk to BFAIG on using ObjectHandles and Degrafa to make a simple diagramming application.  This application demonstrates the basics of working with Degrafa, and also shows how easy it is to make an interface that allows users to resize and move objects on screen.

The example application has source attached, and is licensed under the MIT license.  I hope someone can take it and make some really great application out of it.  Please drop me an email if you do!

I recorded a practice-run I did, so here it is…

 

XML Facade instead of value objects?

I have a project I’m working on where it’d be great if older versions of the software preserved information in the XML file format that it didn’t understand. For example, imagine if Version 1 (V1) of the software had this for a file format:

<data>
<value>1</value>
</data>

Now imagine if V2 of the file added an attribute

<data>
<value type=”number”>1</value>
</data>

It’d be great if you opened that second file with the V1 software and then saved it again, it would preserve the stuff it didn’t understand. Unfortunately that’s not how I usually write my value objects. Usually I do something like:

public class ValueObject{public var value:Number;public static function fromXML( xml:XML ) : ValueObject{var v:ValueObject = new ValueObject();v.value = xml.value;}

public function toXML(  ) : XML{var xml:XML = <data>;xml.value = value;return xml;}}

As you can see, anything in the file that it doesn’t understand is lost. But what if we followed a facade pattern for our data objects and did something more like this:

public class ValueObject{protected var source:XML;

public static function fromXML( xml:XML ) : ValueObject{v.source = xml;}

public function toXML() : XML{return source;}

public function get value() : Number {return source.value;}public function set value(val:Number) : void {source.value = val;}}

They both have the exact same API, but the second one will preserve XML attributes (or even nodes) that it doesn’t understand.

What about AMF based projects, especially when passing rich objects with a Red5 server? I know there’s a pretty seamless mechanism in place if properties aren’t known, but how do you get those unknown properties back to the server?

What other solutions or best-practices do other people follow for solving this issue?

Typed Actionscript Arrays? Yay!

I saw it over here… http://aralbalkan.com/1072

Seems the next player might have typed arrays?

That was #10 on my “Top 10 Flash/Flex requests” Whooohooo! 9 more to go :)

SWC Refreshing

So I got the ant build for the library working with my code-behind thing from yesterday. Unfortunately it has the same results as building the library in a separate project. It seems Flex Builder won’t refresh it’s SWC’s until you either open the config dialog or restart.

Has anyone else found a way to compile a SWC, and have those results immediately apparent in the design-mode view of an app?

Code in front

I’m new to the whole Flex world. One of my first questions was to figure out how to link the components that I laid out in the mxml editor with the classes I created to control those components.

I googled around and found the way most people advocated to do it is with code behind. It consists of creating a base class in actionscript, then extending that class with mxml. This immediately struck me as an odd way of doing it, but I pushed on. It works pretty well.

But I’ve had this nagging feeling that it’s doing things backwards. I have a component that I want to extend the functionality of through code. Why am I doing that inheritance in the opposite direction?

Trolltech makes a great development framework that I’m far more familiar with. They have a .ui format that generates c++ code in exactly the same way that flex has an .mxml format that generates actionscript code. But I think the Trolltech guys got it right just a little more than the Adobe guys. Their .ui generates the base class for you to extend.

So I put my “nagging feeling” and my experience with QT together and have come up with what I will call “Code in Front”.

Code in Front Step 1
Create your component in mxml. Here’s my example:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:VBox xmlns=”*” xmlns:mx=”http://www.adobe.com/2006/mxml”>
<mx:Label text=”Hello World” id=”mLabel”/>
<mx:Button label=”Goodbye” id=”mButton”/>
</mx:VBox>

Save this file as “TestComponentBase.mxml”. This way, a class named “TestComponentBase” will automatically be generated during compile time. We’re going to extend that class in the next step, hence the term: “code in front”

Code in Front Step 2
Create your actionscript class by extending the mxml class. Mine looks something like this:
package
{
import mx.events.FlexEvent;
import flash.events.MouseEvent;

public class TestComponent extends TestComponentBase
{
public function TestComponent()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, init);
}

protected function init(event:FlexEvent) : void
{
mButton.addEventListener(MouseEvent.CLICK, onButtonClick );
}

protected function onButtonClick(event:MouseEvent) : void
{
mLabel.text = “Goodbye World!!!!”;
}

}
}

Code behind step 3:
Use your new component in your application.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”horizontal” xmlns:ns1=”*” width=”423″ height=”400″>
<ns1:TestComponent />
<ns1:TestComponent />
<ns1:TestComponent />
</mx:Application>

And that’s all there is to it. Easy, elegant, makes sense to me.

But there is one huge downfall to this. When using flex builder in design mode to lay out your controls in your app, this is all you see for the above mxml:

That’s losing way too much of the functionality of the tools for most people and I agree. Luckily, there is a way to fix this. First, create a new project based on a Flex Library. Then, inside that project create a new directory from the flex ide. Call that directory “src” and click the “advanced” button. Link that directory to your source directory from your “main” project. You should see something like this:


From this newly created project, right click your component class (TestComponent.as in my example) and select “Include class in library”. Assuming “Build Automatically” is checked off in your project menu, you will shortly get a brand new .swc inside your bin directory.

Now, go back to your “main” project. Right click the project, go to properties. Select “Flex build path” on the left. Select the “Library path” tab on the right. Click the “add swc” button and add that swc we just made in the above step.

Now, go back to your application mxml, go into design mode, click the refresh button, and bamn:

You can now edit the actionscript all you like without problems.

But now a new problem arises. If you edit the mxml base component, and then go back to your application mxml, those custom components are just blank boxes again. Relaunching flex builder fixes this. Going to the properties of your project (not changing anything) and clicking “ok” also fixes it. But that’s a big inconvenience and is something worth investigating to fix.

I’m pretty sure I can replace the whole second-project thing with an ant build task and make that automatically build by installing ant into flex builder. That will most likely fix the problem.

For the record, I’m not claiming to have come up with some novel way of doing things. This idea is so obvious that it’s very likely that someone somewhere else has had it before.