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}"/>
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).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With