Automatic Casting Using XML

I am loving the E4X stuff in ActionScript 3. Once you get your mind wrapped around it and if you don’t have to deal with namespaces it is very nice. Namespaces can be useful too, I just wish you could turn a namespace-agnostic-mode on and off. I just discovered a cool trick I thought I’d share with you. I found it when I was iterating through the properties of two objects to test if they were equal. The properties of one object were all Strings because it was parsed from a comma-delimited file. So, some Booleans or Numbers were strings of "true" or "10.5" while their counter-parts I was testing against were the real true and 10.5. You’d think to accomplish this you’d need a bunch of if statements that cast the values to the same type and test them. That could get messy. And what about casting Booleans. Has to be manual since there is no parseBoolean method. And NULL, same thing. So, I tried using E4X XML to test it. Doing this:

trace("true" == true, XML("true") == true); 

Gives you this: false true This is because E4X will do automatic casting for you. Wonderful! Now I had my solution. Just cast all values to XML first before comparing it (or assigning it). Works with Numbers (of all types), Booleans, and Strings.

AIR Active Record

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

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. <mx:Button label=”{strings.ok}”/>) and when my XML is loaded and the dynamic properties on the strings object are set, my components will automatically update.

Working on Cascade

So I know I haven’t written in awhile. I’ve been busy working on a web content management system (CMS) I hope to get my business off the ground and running. I’m pretty excited about it. About 49% of the people I talk to about it aren’t sure what the difference is between Dreamweaver and a web CMS. They’re the non-web developer people like my family and some of my friends. Another 49% (the web guys) aren’t sure why I would choose such an established market to make a product for and even after I explain my marketing strategy still scratch their heads over it. They are still nice about it and wish me good luck. They’ll all see why here soon! So, I’ve been sort of waiting around for Cascade (my CMS) to be done and then continue blogging, but that’s taking too long, so I’ll keep writing until it’s done. Then I’ll be moving my site over to withincode.com and using Cascade to manage it all.

Seeking Perfection

I find myself always seeking for the perfect code, the perfect library, the perfect system. Is it healthy? My employer might rather a code monkey which will mass produce web applications. Sure, the code would be a pain to maintain, but we would make more money. Or would we?

I was contracted by my company for awhile to one of their clients. While I was there I started making my own JavaScript tree menu in my spare time. The developers I worked with asked why I would do something like that. There are already a handful of tree menus free for download on the web. I was doing it because, one, I thought it would be fun (they thought I was crazy), and two, none of the tree menus gave the functionality I sought. I ended up making a tree menu that is really nice, has a context menu, and works well with Ajax. I have used it in several projects.

I also started building a PHP framework around that time. Pherret was created in my spare time (another “painful” project my coworkers said) because I thought it would be fun and because I didn’t like what was already out there? Arras Template, which became part of Pherret, is a unique and fun templating system. And finally I put together my own JavaScript Ajax library, borrowing some from others like Prototype.

Not being content to use others’ code, others’ frameworks, and others’ hard work is my downfall.

Or is it? I am now the expert at my company in JavaScript. I know the inner workings of what a framework needs to deal with. I know what a template system needs to be able to do, and some of how to do it. I have gone from knowing very little about programming (just 2 years ago) to being a well-versed and well-rounded developer. The drive that has me writing things that have already been done, or even just the drive that has me developing in my spare time instead of just in the workplace is what has made me valuable as a developer. I suppose we call the type a “hacker”. The person who loves to get their hands into code as a hobby.

I’m glad I’m a hacker. I program in a way that my code becomes better and better, day after day. My applications become better layed out. Better organized. Easier to maintain.

Writing (Beta)

MyStickies has had a great launch. It has been pretty successful. Quite a few users have a hard time setting it up, but once that is done, they enjoy using it. As of the time of this writing 17,982 accounts were created, 119,400 notes have been placed, 31,108 notes are currently sitting on pages accross the web and we are getting between 50 and 100 new users each day. MyStickies started off with a “Beta” tag on it, but went public without the beta label though it probably should have kept it. Unicode support was broken, usability was lacking, and databases had moments of down-time.

Why did we remove the beta label? Because Jason Fried said we shouldn’t. First was a blog post from 37 Signals. They noticed how many new Ajax applications were releasing with the “beta” label. Then in TechCrunch’s article “Don’t Blow Your Beta” he left this comment:

“There’s a better way not to blow your beta: don’t launch one at all. Public betas are rediculous. If your product is public, it’s not a beta, it’s a release. Take responsibility for your product. “Beta” only passes the buck to your customers — outsourcing your pain to them.

“Get over Beta already.”

And then Ajaxian released the article “The Importance of a Good Beta” and added to the above quote from Jason.

What’s wrong with beta? Nothing. Might not be a good idea to charge for a beta product. I don’t think 37 Signals should release BaseCamp or other commercial products with a beta label and charge their customers for it. But that doesn’t mean a free public beta is a bad thing. It is actually a very good thing to be able to get customer feedback early, have users test accross a large variety of platforms, OS’s, and machines, and rethink the goals of the product before all the cleanup work has been done.

If there is nothing wrong with a free beta, why did we change it? We didn’t want people to think we were not taking responsibility for it. We wanted MyStickies to be successful, and we thought people would listen to Jason and not use it if it said “Beta” on it. Were we right? Don’t know. People seem to use Gmail even though it has Beta on it’s logo.

It all comes down to the power of writing. The 37 Signals team has some very good writers and even emphasize that you need good writers to be successful. They have a massive following of people who read their blog and swear by what they write. I would bet most of their products successes are because of their great writing. Marketing themselves on the web. In order to be a have read blog, to generate a hype, to drive a web-application to be successful, good writing is important, maybe essential. My writing needs improvement for sure.

« Previous Page — Next Page »