Coding Advice

If you have been following along with the Flex 2 announcements, you have noticed that Flex 2 will be using ActionScript 3. Flex 1.5 uses ActionScript 2. The benefits of ActionScript 3 are too numerous to name here, but I thought I’d give a couple of pieces of advice for coding Flex 1.5 in anticpation of moving to Flex 2.

This advice should get you used to coding in AS 3 “mode” and reduce the number of changes you will have to make.

Data Type Everything

Make sure everything has a data type. For example, change:

var n = 4;
to
var n:Number = 4;

If you are unsure of what type something should be, make it an Object.

The downside is that not everything can be easily typed. For example, while there is an Event type, once you identify a variable as being of that type you cannot add your own properties to it. Flex 2 will overcome that by providing many more pre-defined classes.

If you run into a problem, then just leave the data type off for now and fix it if you port the code to Flex 2.

Events

In Flex 1.5 you can conveniently dispatch events by writing Objects inline:

dispatchEvent( {type:”select”, code:”Something” } );

This will not be allowed in Flex 2. Instead, start writing the code like this:

var event:Object = new Object();
event.type = “select”;
event.code = “Something;
dispatchEvent(event);

Public and Private

In Flex 2 you will be required to identify the scope of variables and functions. Right now you can get away with:

var dp;
function initApp() {
dp = new Array();
}

In Flex 2, this should become:

private var dp;
private function initApp() : Void {
dp = new Array();
}

Bindings

In Flex 1.5 you can bind to variables without any special syntax. For example:

var dp:Array = [{name:”George”,age:18},{name:”Holly”,age:23},…];
<mx:DataGrid dataProvider=”{dp}” />

In Flex 2 you need to identify those variables which will be used in data binding by using the [Binding] directive.

But you can use [Binding] now and just get used to making the identification:

[Binding]
var dp:Array = … ;

The [Binding] directive is harmless in Flex 1.5.

Conclusion

I know these seem minor, but if you get into the habit now, you’ll be better off if you try and port your application to Flex 2.

5 Responses to Coding Advice

  1. JesterXL says:

    As far as I’m concerned, everything is a re-write.

    …however, thanks a lot for the Binding 411, didn’t know that one, thanks!

  2. merhl says:

    Also make sure you change xmlns:mx=”http://www.macromedia.com/2003/mxml to xmlns:mx=”http://www.macromedia.com/2005/mxml

  3. Peter Ent says:

    You only use the 2005 uri when coding Flex 2. This entry was about what you can do in Flex 1.5 to get ready for Flex 2. Or rather, what habits to get into so Flex 2 will be easier.

  4. Scott Barnes says:

    Amen Jess.

    I’ve been lucky in that i’ve abstracted a lot of the in-built methods such as createChild() for my own, well thanks to an early heads up on MM.

    Syntax wise, that was a pain in the royal butt especially if you use trace a lot (now having to import that) when doing code port.

    I’ve also not used mx name space for a lot of my work but used my own, which basically is empty class files that extend the mx framework. I wanted to keep things as is, and if FLEX 2.0 was as scarey as i thought it was going to be, i might stand a better chance at salvage then i would normally.

    That all being said, i failed 🙂

    Its re-write city and bless all for trying heh.

  5. Ruchir Vaishnav says:

    Hello Peter

    I want to display paragraph contents on a webpage with a “justify” alignment. So, I have created a text control in mxml but dicovered that the textAlign property only supports three values – ‘right’,’left’ and ‘center’

    To workaround this problem, I used htmlText property with html tag align = ‘justify’ in my mxml script.. As shown below.

    textField.htmlText = ” Headquatered in London…………..”;

    This does not work. I also tried as shown below

    textField.htmlText = ” Headquatered in London…….”;

    Unfortunately this does not work either.

    However, If I pass any other value other than ‘justify'(Right,Left,Center) it works. This means that even html formatting for suported tags in mxml does not work and I think this is a bug becuase and html tags are supported in mxml.

    Is there any way to get this done in mxml? I need to have a ‘justify’ alignment for all my contents of the webpage and other options are not acceptable at all. PLEASE HELP!!

    Other alignments such as ‘right’, ‘left’ and ‘center’ would make webpage contents ugly and not acceptable at al;l.

    Thanks,

    Ruchir