How to name it ?

Not so FLEXible with FLEX

Posted in Adobe, Flex, Technical by Srini on December 9, 2009

Having been on the Visual Studio side of the fence for more than 3 years, Microsoft eclipsed any opportunity for the need or necessity of a secondary IDE. When opportunity knocked on to work with Adobe Flex, never did I know that I would gasp for my breath at some stage. As we know every language and environment comes with its own pitfalls and Flex has the share of its own, which at the current stage might leave you gasping especially if you have worked earlier on .NET with Visual Studio. Here are some efforts to highlight what you might experience, and suggestions if any to overcome the same.

For starters, the current Go-Live development version of Flex (as of this writing) is Flex 3 whose SDK is pretty much a free download , but if you want to leverage the advantage of the classic IDE support you may need to invest on a Flex Builder 3 license. A limited day evaluation of Flex Builder IDE is available for free on Adobe Downloads if you wish to jump-start. The Flex Builder IDE is built over the fairly known Eclipse IDE and if you have worked earlier on Eclipse (which was perhaps not my case), you might have probably experienced some of the IDE related points discussed here.

A Date with Dates

The Date objects in Flex are treated differently. To jump with an example straight away, if you try to write something like this hoping to set a Date to 28th Jan 2009, you may very well be wrong.

           var myDate:Date = new Date(2009,01,28);

Flex month index is a zero based index, meaning a month “00” will mean January and “01” will be pointing to February! So beware, when you execute the above statement you are setting the Date to 28th Feb 2009. The same holds good while you are debugging and “Expression Watching” any Date Objects. A Watch on the above will give you something like this. Note the month value!

There’s another interesting thing which needs attention here. Date Manipulation in Flex doesn’t get simple either. If you want to move one month back from a date, say 31st March 2009, we generally use AddMonths(-1) in .NET or DATEADD() functions in SQL. I haven’t come across any direct Date Manipulation functions in Flex and one of the ways to do it is provided below.

     var myDate:Date = new Date(2009,02,31);
     var day:Number = myDate.getDate();
     var month:Number = myDate.getMonth();
     var year:Number = myDate.getFullYear();
     var newDate:Date = new Date(year,month – 1,day);

Alternatively we can also do a myDate.month = myDate.month – 1 and use the same myDate object.

Note that we have set the starting date to be 31st March 2009 (A month index of 2 is March). So if you are expecting the newDate value to be 28th Feb 2009, well you may be wrong! The value we get, very interestingly is 3rd March 2009. On doing a month – 1, Flex does not care about which month or which year we are currently in, instead it blindly goes back to 31st Feb 2009. Since Feb 2009 ends on 28th, the difference of 3 is added to the subsequent month and we end up with 3rd March.

We should generally be writing our own logic to handle such scenarios, if need be. Nevertheless, Date manipulation logic finds almost no support in Flex 3 native libraries. DateUtil functions on the web may probably solve some deal though, so if you are on the hunt for one, you can probably start with here – http://code.google.com/p/flexdateutils/wiki/DateUtils

So, enough on dating? How about doing an event – perhaps a pop-up party !!

Handling Events on Popups

If you have found that sometimes when you add an EventListener to a class and it never listens, never worry – you have some company. The event listener mechanism needs a special mention whenever it is raised from a pop-up window.

For a very quick overview, we have an Application class in Flex underneath it we get all the UIComponents. So within any of the component if you say this.addEventListener() it can bubble across till the Application layer making sure that all components within the Application scope are ready to listen. However Popups is an exception, because a Popup is a child of the SystemManager and not the Application class. Treat it as a sibling to Application, who are now probably not on good terms. So you gotta register the complain to their dad for them to fairly work!

To put simply, if the addEventListener() is from a Popup screen then this.addEventListener() is not going to work, instead use Application.application.systemManager.addEventListener() to take benefit.

I came across this situation once, and this is the place where I got the solution – http://www.munkiihouse.com/?p=45

So after dating and partying here is some real stuff to whack your brains!!

FLEXible Maths

If,   a = 100.1;
       b = 200.7;
       c = a + b;

What is c?

You don’t get a free beer if you say 300.8, because Flex thinks it’s 300.79999999999995. Even more strangely, if you change a and b to 100.7 and 200.1 respectively, it calculates perfectly to 300.8. This round-off error gets introduced for some strange odd decimal equivalent for which I haven’t figured out the reason yet. But for the fix, here is something that can help.

var objnumberFormatter:NumberFormatter = new NumberFormatter();
objnumberFormatter.rounding = NumberBaseRoundType.NEAREST;
objnumberFormatter.precision = 1;
objnumberFormatter.format(c);

The above snippet will emit a string for a single decimal precision rounding of the result to the Nearest equivalent.

Hope it helps!

Keep looking out, I should be soon adding the IDE related tips.

Leave a comment