AIR Active Record

Jacob Wright
December 19th, 2007

I’ve been working with AIR quite a bit since my side project is in AIR. I thought it would be pretty cool to create an active record implementation in AIR since I’ve got one on the server side.

I have to use synchronous database connections for it so that everytime I access an object’s related properties I don’t have to use a callback, though it could be refactored to do that. Not my idea of fun though, and after an excellent presentation by Jason Williams, “Working with Persistent Data in AIR,” where he showed how fast it was to retrieve data from an AIR database I figured it would be just fine.

Currently I don’t have metadata allowing you to define how every property maps to a field in the database, nor do I have many “special case” hooks for this and that. I figured that any AIR app you write will be using a fresh database and not a legacy system. How many desktop apps using a SQLite database will you be updating with AIR and using the same database file? Maybe I’m wrong, but I don’t think so.

I also added a piece that you can run which will create and update database tables based off the ActiveRecord object’s properties. So you have a nice update mechanism for the database when you update your app using the updater api.

I’ll be open-sourcing the code when I have time (isn’t that always the case), but you can get the code now, test it out, and let me know what enhancements you’d like to see. Sorry, no documentation currently. If I like the ideas or they’re requested enough I’ll be sure to add them in. :)

Oh, and when you unzip it you’ll see “flight” is the package Tyler (my twin) and I are using for our projects. It started when he and Rob Taylor started a component set in AS2 under the name but AS3 and Flex 2 came out and they never finished. Maybe an AS3 set will happen someday.

Update: AIR Active Record is open sourced.

ActionScript 3 Bindable Dynamic Objects

Jacob Wright
June 26th, 2007

So I've been working heavily in Flex lately (and AIR). Been making my own components and building user interfaces for Cascade. For a project I'm working on at work it implements its own localization. It uses a method called getString('myString') to load a string up from the XML strings file.

This was not good for interfaces with lots of strings in it. It required giving every label, every input, every button and id and then after the strings were loaded assigning them all. One of the MXML files I was looking at had over 50 strings to be set and the listener to the string loading ended up being half the page almost! (maybe I exaggerate)

I like binding, and so I set about to create a dynamic object that implemented binding. Putting a [Bindable] tag at the top of a dynamic class didn't do it. So I made it a proxy and still no go. Finally, after thinking about how it works under the hood, I was sure I knew how I could make it work.... and it did.

I specified the event which would trigger the binding updates to "propertyChange", the default. This gives me the responsibility of dispatching the event after a bound property is set. Then, implementing IEventDispatcher so that I COULD dispatch an event, and extending Proxy so that I could make sure it happened after setting a property, I came up with the following solution:

package flight.utils
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;import mx.events.PropertyChangeEvent;
    import mx.events.PropertyChangeEventKind;use namespace flash_proxy;
   
    [Bindable("propertyChange")]
    dynamic public class BindableObject extends Proxy implements IEventDispatcher
    {
       
        protected var strings:Object;
        protected var eventDispatcher:EventDispatcher;
       
        public function BindableObject()
        {
            strings = {};
            eventDispatcher = new EventDispatcher(this);
        }
       
        flash_proxy override function getProperty(name:*):*
        {
            return strings[name] || name;
        }
       
        flash_proxy override function setProperty(name:*, value:*):void
        {
            var oldValue:* = strings[name];
            strings[name] = value;
            var kind:String = PropertyChangeEventKind.UPDATE;
            dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE, false, false, kind, name, oldValue, value, this));
        }
       
        public function hasEventListener(type:String):Boolean
        {
            return eventDispatcher.hasEventListener(type);
        }
       
        public function willTrigger(type:String):Boolean
        {
            return eventDispatcher.willTrigger(type);
        }
       
        public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void
        {
            eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }
       
        public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
        {
            eventDispatcher.removeEventListener(type, listener, useCapture);
        }
       
        public function dispatchEvent(event:Event):Boolean
        {
            return eventDispatcher.dispatchEvent(event);
        }
    }
}

So I can create an object, "strings", and bind all my buttons and labels to properties of the strings object (e.g. ) and when my XML is loaded and the dynamic properties on the strings object are set, my components will automatically update.

« Previous Page