View Full Version : I need a MXML constructor
testerman
04-20-2009, 06:34 PM
Obviously there are times when you NEED a constructor. Why Adobe decided that MXML componets don't need one is beyond me. I NEED to create an MXML component that has access to a variable that was set when the MXML component was called not when it was completely created. Is there a way to do this in MXML? Or do I have to scrap all that and go straight to AS?
Thanks
wvxvw
04-20-2009, 07:21 PM
Well, no, you cannot pass argumets to constructor throug MXML.
But, you can have default arguments eg.:
public function ConstructorOFCustomComponent(param:int = 25)
{
super();
this.param = param;
}
Also you may be sure that the first instance method that will be called in IMXMLObject is
initialized(document:Object, id:String):void {....}
You may override it in a way it will try to get that parameter from the document (parent) object.
In some cases you may have a mixin function (it is static function of that same IMXMLObject that you design to call each time before you create a new instance of that object. This is somewhat cumbersome, and isn't save as you may simply forget to do that... or call it to late.
public function ConstructorOFCustomComponent()
{
super();
var params:Object = getConstructorArguments();
this.param0 = params.param0;
this.param1 = params.param1;
this.param3 = initializer.param3;
}
public static function getConstructorArguments():Object
{
return { param0: "foo", param1: "bar" };
}
// or
public static var initializer:Object = { param3: "foobar" };
But, honestly, the more I use MXML the less I find myself in need to pass any arguments to the constructor. For most part the constructors of my MXML components look like this:
public function TheConstructor() { super(); }
Actually... can you give an example where you cannot do without constructor arguments?
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.