Flex Components: MXML vs. ActionScript
Jun/081
One question that has constantly nagged me during my conversion from Flash to Flex development is, “Do I write this thingamajig in MXML or ActionScript?” The question is usually answered by the type of component that’s being written. If it’s a visual component, I’ll write MXML otherwise, I’ll write the whole thing in ActionScript.
This kind of reasoning was satisfying in the beginning but as I gain more experience with component development in Flex, I’m finding that it may not be as clear cut as I once thought. In fact, it wasn’t until I glanced at this article on Adobe’s Developer Connection site that I’ve since refined my decision-making process.
Here are the two points mentioned in the article:
- When you create composite controls declaratively, MXML makes it easier to create and lay out child controls.
- When you modify the behavior of components, that is, override their methods, you typically use ActionScript.
As simple and obvious as they may sound, they’re quite powerful when trying to decide in which direction to take your development. As stated in the article, most of the time you’ll be using a combination of the two. One way to do that is by using a technique called “code behind” which is something I’m working on right now…
UPDATE: After playing with the code behind technique mentioned in the article it seems obvious to me that it’s the way to go in most cases. It’s also very satisfying to separate form from function in a sensible manner. Unless, you’re developing a very simple component you should probably get in the habit of writing core logic (as a class) in ActionScript and extending it via the root tag in MXML.
