PDA

View Full Version : What does the binding tag really do, when you're not looking?


NeonPlatypus
05-04-2007, 08:56 PM
Ok, I know what it's used for, but what does it really do behind the scenes? As far as I can tell it's turning my classes into Event Dispatchers.

If you create this simple Flex app you can see that the [Bindable] tag inside the House class has turned it into an Event Dispatcher. Check the trace commands in the MXML:

package
{
[Bindable]
public class House
{
public var rooms:Number;
public var windows:Number;
public var garage:Boolean;

public function House() { }
}
}



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" layout="absolute" >
<mx:Script>
<![CDATA[
import mx.events.PropertyChangeEvent;
import mx.binding.utils.ChangeWatcher;

[Bindable]
public var house:House;

private function init():void
{
house = new House();
trace(house is IEventDispatcher); // traces true
trace(ChangeWatcher.canWatch(this, "house"); // traces true
ChangeWatcher.watch(this, ["house", "rooms"], roomsChanged);
house.rooms = 5;
}

public function roomsChanged(event:PropertyChangeEvent):void
{
trace(event.target.rooms); // traces 5
}
]]>
</mx:Script>
</mx:Application>


Notice how "house is IEventDispatcher" returns true. And, if I try to add IEventDispatcher methods into House I get a compile-time error telling me of a conflict.

Now, if you remove [Bindable] from inside the class then "house is IEventDispatcher" return false, but strangely ChangeWatcher claims it can still watch for events from it. In reality it can't, as the handler is never called when that property changes. It lies!!

But it gets weirder!!

If you remove [Bindable] from the MXML script (just above "public var house:House") but keep it inside the House class, then ChangeWatcher claims is can't watch the property, but in fact it can!! and the handler is called whenever the property changes.

So what the hell is the [Bindable] tag really doing behind the scenes, and what is ChangeWatcher basing it's assumptions off?

dr_zeus
05-07-2007, 06:12 PM
When you put [Bindable] at the top of a class, it makes all of the properties inside the class bindable. It's a shortcut so that you don't need to put [Bindable] on each property individually. This is very different than making an instance of that class bindable.