Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't mxml support component constructors?

Why doesn't the Flex framework's mxml language support a constructor for components or accept constructor arguments for components? It's as far as I know not possible to declare an ActionScript object in mxml if it takes constructor arguments. I'm curious about the reason. Is it a design choice by Adobe or related to how declarative languages work? For example, why not allow:

<myNameSpace:MyComponent constructor="{argArray}"/> 
like image 580
onosendai Avatar asked Sep 22 '11 05:09

onosendai


2 Answers

You can read IMXMLObject help API for more information about your question. They're not telling exactly why an mxml doesn't support constructors, but it says that you must control your mxml component throught its lifecycle events: preinitialize, initialize and creationComplete.

I suppose that's a design decision, considering that an mxml gets translated directly to AS3 code (you can compile your application adding keep-generated-actionscript=true and see what it produces).

like image 57
Lasneyx Avatar answered Oct 23 '22 08:10

Lasneyx


Even if a class is defined in MXML, it is possible to implement a constructor via instantiating an instance variable as follows. This will get called before various events like "preinitialize" or "creationComplete" are dispatched.

<myNameSpace:MyComponent>
  <fx:Script>
  <![CDATA[
     private var ignored:* = myInstanceConstructor();

     private function myInstanceConstructor():* {
         // Do something - called once per instance
         return null;
     }
  ]]>
  </fx:Script>
</myNameSpace:MyComponent>

Moreover, class variables can be initialized in a similar way as follows.

<myNameSpace:MyComponent>
  <fx:Script>
  <![CDATA[
     private static var ignored:* = myClassConstructor();

     private static function myClassConstructor():* {
         // Do something - called once per class
         return null;
     }
  ]]>
  </fx:Script>
</myNameSpace:MyComponent>
like image 26
CQ Bear Avatar answered Oct 23 '22 06:10

CQ Bear