Mark, this is an interesting thought exercise. Here are a few ideas. (Note,
however, that they're all impractical. I suggest you should simply use
setters or initialization methods as the above responders suggested.
Moreover, you mention that you "cannot set someProp at the design time
because someValue isn't available yet" - that's what Binding is great at.)
1. if it's not too hard, you could turn the class from Obj1.mxml to
Obj1.as, then use the hacky method of setting a static property before the
constructor is called. I.e.:
Obj1.someVariableThatReallyReallyNeedsToBeAvailableAtConstruction = "test";
_obj = new Obj1();
and inside Obj1.as you would have
public static function set
someVariableThatReallyReallyNeedsToBeAvailableAtConstruction(value:String):void
{
_importantVariable = value;
}
public function Obj1()
{
super();
trace(_importantVariable); //"test"
}
This way you can still use Obj1 in mxml.
β2. If, on the other hand, you don't need to use Obj1 declaratively in
mxml, you could create a βnew, very simple class called SuperObj1.as that
extends Obj1.mxml, as such:
β
public class SuperObj1 extends Obj1
{
public function SuperObj1(someInfo:String)
{
trace(someInfo);
super();
}
}
β
Notice that SuperObj1 takes a constructor argument, which you then can use
with Obj1's public functions / properties, etc. (Though not with Obj1's
children, which won't be created at this point.)
3. Or, if indeed you're not going to use Obj1 in mxml, then simply make it
a pure actionscript class and add all the constructor arguments you need.
But to 1) use the component in mxml and 2) want to write constructor code
seems to be impossible. Unless, of course, you keep the compiler-generated
as files, then recompile the project after you figure out how to hack the
construction of Obj1 by the Flex framework to use your desired parameter.
Not recommended ;)
On 27 September 2013 16:46, Alex Harui <[email protected]> wrote:
>
>
> On 9/27/13 8:38 AM, "mark goldin" <[email protected]> wrote:
>
> >Well according to the docs Flex calls the IMXMLObject.initialized() method
> >after it initializes the properties of the component.
> >Isn't it too late for what I need?
> My point is that, if you want to use MXML, then you have to give up on
> constructor-time work and write the AS classes to expect work to be done
> later. There is no solution that will give you constructor-time property
> values.
>
> -Alex
> >
> >On Thu, Sep 26, 2013 at 10:24 PM, Alex Harui <[email protected]> wrote:
> >
> >> Hi Mark,
> >>
> >> That is a limitation of MXML. If you have an AS class that is based on
> >> UIComponent, you are encouraged to use the lifecycle methods like
> >> invalidateProperties/commitProperties instead of doing work off the
> >> constructor. If you have an AS class not based on UIComponent, a common
> >> pattern is to implement IMXMLObject and do all of the work in the
> >> initialize method so it can be used in MXML, and then when instantiating
> >> in AS, you will do:
> >>
> >> var _myClass_mxml:MyClass_mxml = new MyClass_mxml();
> >> _myClass.initialize(document, "_myClass");
> >>
> >> -Alex
> >>
> >> On 9/26/13 7:14 PM, "mark goldin" <[email protected]> wrote:
> >>
> >> >No, that does not work because what I am instantiating is an mxml class
> >> >that is based on MyClass:
> >> >MyClass_mxml:
> >> ><ns:MyClass>
> >> >...
> >> ></ns:MyClass>
> >> >
> >> >and then in AS:
> >> >var _myClass_mxml:MyClass_mxml = new MyClass_mxml(); This will not
> >>take a
> >> >parameter to a constructor.
> >> >
> >> >
> >> >On Thu, Sep 26, 2013 at 8:55 PM, Mark Kessler
> >> ><[email protected]>wrote:
> >> >
> >> >> Oh I see your using AS to create an instance of it.
> >> >>
> >> >> This would work for accessing that property...
> >> >> var newMyClass:MyClass = new MyClass()
> >> >> newMyClass.myProperty = object being passed.
> >> >>
> >> >>
> >> >> But since your using AS you could pass it in the constructor now.
> >> >>
> >> >> public class MyClass
> >> >> {
> >> >> //Constructor
> >> >> public function MyClass (myProp:Object)
> >> >> {
> >> >> //init stuff using whatever you passed.
> >> >> }
> >> >> }
> >> >>
> >> >> var newMyClass:MyClass = new MyClass(My prop or object or whichever
> >> >>matches
> >> >> your needs.)
> >> >>
> >> >>
> >> >>
> >> >> -Mark
> >> >>
> >> >>
> >> >> On Thu, Sep 26, 2013 at 9:01 PM, mark goldin <[email protected]>
> >> >> wrote:
> >> >>
> >> >> > That is not exactly what I have. Yes, I too have an mxml class
> >>based
> >> >>on
> >> >> > another AS class. But I am creating an instance of my mxml class
> >>like
> >> >> this:
> >> >> > var newMyClass:MyClass = new MyClass();
> >> >> > So, how can I pass a value for myProperty to newMyClass? My
> >> >>understanding
> >> >> > as soon as var newMyClass:MyClass = new MyClass(); executes all
> >> >>children
> >> >> on
> >> >> > newMyClass will be created but I need to have myProperty before
> >>that.
> >> >> >
> >> >> >
> >> >>
> >>
> >>
>
>