Ok, you have to write a class for a Flex project, so do you use MXML or Actionscript? What I’ve often seen in Flex projects is MXML everywhere, even when a simple Actionscript class would have done the trick, hence this post. I understand the urge to use MXML, but there are a few caveats that make it a deal-breaker that I’m going to look at here.
– no parameters in the constructor. When you design a class, parameters in the constructor indicate the parameters without which the class cannot function. As far as I know, there is no other way to push this information towards the person integrating the class, except maybe with a doc that won’t be read unless you twist their arm. What is also nice about parameters in the constructor is that you don’t have to worry about them being set, because they are available for the whole lifespan of the class.
– Limited inheritance. If you created an MXML file and want to add elements in a sub-class, you can’t. Not only that but it doesn’t break at compile-time but at run-time with an incomprehensible debug message.
– increased compilation time. Remember, MXML is converted to Actionscript. So it takes longer to compile. As a side note, the generated Actionscript is really ugly, but that would be for another post.
– no internal classes for MXML files. Ok, this is nitpicking, but when you gotta do it, you gotta do it and you can’t.
After all that, the one place where I feel MXML shines and is really justified is when you need to do some floating layouts with boxes. I’ve tried doing it in Actionscript and it’s just completely unreadable compared to MXML.
So in conclusion, my rule of thumb is the following: If it doesn’t at least have 2 levels of imbrication of boxes, it’s probably better to go straight to Actionscript.
by Kris Hofmans
31 Mar 2009 at 23:05
I really like code behind pattern for my flex pages. Usually gives me best of both worlds, no -tag in the mxml, all the event handlers in the base class, nice and pretty
And it keeps all the layout declared in mxml. Hell you can even give the mxml to some designer whithout having to worry about him completely messing up the page!
by admin
01 Apr 2009 at 09:15
Hi Kris,
thanks for the tip I had never heard of this approach. So I looked for it and found this which seems a good writeup: http://blog.vivisectingmedia.com/2008/04/the-flex-code-behind-pattern/
I like the idea but I’ll have to give a try in a real world situation. That would make a follow-up post I guess.
Ariel
by Tink
09 Apr 2009 at 04:40
We use MXML for views
by admin
09 Apr 2009 at 09:09
Hi Tink,
Is it really better than writing your view in actionscript though? Unless you use nesting, I still don’t think so.
Ariel
by Tink
10 Apr 2009 at 01:27
Yeah its much quicker, and nice and tidy.
It also easier to set up bindings, implement states, transitions and effects.
Defo worth getting in to. Once your familiar with it, you never look back.