September 18, 2003
A script-only formatter in FMX2004 pro
After discovering how you can bind component values in Flash MX 2004 pro, I immediately wondered how to implement the "formatter" functionality via only script. As you may know, I've created a dateFormat function to format a Date object in ActionScript 1, and I was curious to see if this was implemented in FMX2004 or not.
For an overview of what I'm referring to, read this article on Data Binding over at Macromedia's website. I'm not opposed to the menu driven technique displayed in the article. This was a thought experiment to see if I could make use of the formatters via code only. This is what I came up with...
var dataFormatter:mx.utils.StringFormatter; dataFormatter = new mx.utils.StringFormatter( "MM/DD/YYYY", "M,D,Y,H,N,S", mx.data.binding.DateBase.extractTokenDate, mx.data.binding.DateBase.infuseTokenDate ); var today:Date = new Date(); trace(dataFormatter.formatValue(today)); // 09/18/2003
In order for this to work, you need access to the mx.data.binding classes and the mx.utils classes, all of which have been left out of the Classes folder in the "FirstRun\Classes" directory under your Flash MX 2004 installation directory. An easy way to compile the code above is to add these classes to your library. So... where do we get these classes?
If you look in the "C:\Program Files\Macromedia\Flash MX 2004\en\First Run\Data" directory (or something similar), you'll see a file "DataBindingClasses.swc". This compiled swf file contains the information we need to execute the script above.
An easy way to add this to your library is to copy the DataBindingClasses.swc file to "C:\Program Files\Macromedia\Flash MX 2004\en\First Run\Components\Data Components" and then re-start Flash to update the components panel. I've tried selecting "Reload" from the components panel, but the only way I've found to consistently update the panel is to restart Flash.
EDIT - Thanks to Chafic Kazoun of RewindLife.com there is an easier way to add the .swc to your library. In the IDE, select "Window" -> "Other Panels" -> "Common Libraries" -> "Classes". This will open up a library with the DataBindingClasses already inside.
Now, open up a blank .fla document, drag the DataBindingClasses (from the "Classes" library) onto the stage, and copy/paste that code onto frame 1 to see it in action.
Personally, I think I'll stick with my own dateFormat function if I want to format Date values. My version seems to be a little more flexible anyway (month name support, single-digit support, etc), but this process of discovery was enjoyable... Explore explore explore!!

Comments
This is cool. What are these two items:
mx.data.binding.DateBase.extractTokenDate,
mx.data.binding.DateBase.infuseTokenDate
Posted by: Brook Davies at April 15, 2004 01:17 PM