You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by mark goldin <ma...@gmail.com> on 2013/09/26 20:57:21 UTC
Send parameter to mxml constructor
I have an mxml class called Obj1. Somewhere in my code I do:
var _obj1 = new Obj1();
What I need is to provide some variable to Obj1 for its initialization. A
reason for that is because Obj1 has children that need that variable for
their initialization.
But because I cannot send a parameter to Obj1's constructor I dont know how
to solve my problem.
Any idea?
Thanks
Re: Send parameter to mxml constructor
Posted by Sumudu Chinthaka <cs...@gmail.com>.
this is how im doing it
this is my MXMl component with the variable (userName) that i want to set
when i instantiate this object from another XML and i have a helper method
that does it and it return the reference to the caller
<?xml version="1.0" encoding="utf-8"?>
<s:Group width="200" height="200" xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:components="spark.components.*">
<fx:Script>
<![CDATA[
[Bindable]
private var userName:String;
public function initilizeMe(value:String):MyTestComp{
this.userName = value;
return this;
}
]]>
</fx:Script>
<s:Label text="{userName}"/>
</s:Group>
and in AS side i can just create a Object and call the initilizeMe
var myObj:MyTestComp = new MyTestComp().initilizeMe("User Name");
Thanks
Sumudu
On Fri, Sep 27, 2013 at 12:56 PM, Peter Ginneberge
<p....@gmail.com>wrote:
> An often overlooked "feature" of MXML components (extending UIComponent)
> is that they have a data property that can be used for pretty much anything.
>
> // where MyClass is MXML
> var instance:MyClass = new MyClass();
> instance.data = {prop:"value"};
>
> In MyClass you can either override the "data" getter/setter or listen for
> the dataChange event and do what you need to do.
>
> The data property is also bindable, so you can have mxml children inside
> MyClass bind to it.
>
> <mx:Label text="{data.prop}" />
>
> One of the downsides of the data property is data typing (it's types as
> Object) so you may have to do some type casting if needed.
>
> regards,
> Peter
>
> ----- Original Message ----- From: "mark goldin" <ma...@gmail.com>
> To: "users" <us...@flex.apache.org>
> Sent: Friday, September 27, 2013 3:01 AM
> Subject: Re: Send parameter to mxml constructor
>
>
>
> 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.
>>
>>
>> On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
>> <ke...@gmail.com>**wrote:
>>
>> Let me make the example a little more neutral.
>>>
>>>
>>> class being called.
>>>
>>> public class MyClass inherits UIComponent
>>> {
>>> /**
>>> * Constructor.
>>> */
>>> public function MyClass ():void
>>> {
>>> super();
>>> }
>>>
>>> //----------------------------**------------
>>> // My setup property
>>> //----------------------------**------------
>>> protected _myProperty:Object;
>>>
>>> public function get myProperty():Object
>>> {
>>> return
>>> }
>>>
>>> public function set myProperty(value:Object):void
>>> {
>>> if (_myProperty == value)
>>> {
>>> return;
>>> }
>>>
>>> _myProperty = value;
>>>
>>> setupChildren();
>>> }
>>>
>>> protected function setupChildren():void
>>> {
>>> //using the _myProperty to setup the children and marking
>>> anything as being ready and available... events n such.
>>> }
>>> }
>>>
>>>
>>> mxml side
>>>
>>> <ns:MyClass myProperty="The property i'm passing to initialize" />
>>>
>>>
>>>
>>>
>>> -Mark
>>>
>>>
>>>
>>> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
>>> wrote:
>>>
>>> > Are you saying I have to involve the DI framework?
>>> >
>>> >
>>> > On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>>> > mark.kessler.ctr@usmc.mil> wrote:
>>> >
>>> > > Setter Dependency Injection(mxml compatible) vs constructor
>>> Dependency
>>> > > Injection (AS compatible).
>>> > >
>>> > > -Mark
>>> > >
>>> > > -----Original Message-----
>>> > > From: mark goldin [mailto:markzolotoy@gmail.com]
>>> > > Sent: Thursday, September 26, 2013 2:57 PM
>>> > > To: users
>>> > > Subject: Send parameter to mxml constructor
>>> > >
>>> > > I have an mxml class called Obj1. Somewhere in my code I do:
>>> > > var _obj1 = new Obj1();
>>> > >
>>> > > What I need is to provide some variable to Obj1 for its
>>> initialization. A
>>> > > reason for that is because Obj1 has children that need that variable
>>> for
>>> > > their initialization.
>>> > > But because I cannot send a parameter to Obj1's constructor I dont
>>> know
>>> > how
>>> > > to solve my problem.
>>> > > Any idea?
>>> > >
>>> > > Thanks
>>> > >
>>> >
>>>
>>>
>>
>
Re: Send parameter to mxml constructor
Posted by Peter Ginneberge <p....@gmail.com>.
An often overlooked "feature" of MXML components (extending UIComponent) is that they have a data property that can be used for
pretty much anything.
// where MyClass is MXML
var instance:MyClass = new MyClass();
instance.data = {prop:"value"};
In MyClass you can either override the "data" getter/setter or listen for the dataChange event and do what you need to do.
The data property is also bindable, so you can have mxml children inside MyClass bind to it.
<mx:Label text="{data.prop}" />
One of the downsides of the data property is data typing (it's types as Object) so you may have to do some type casting if needed.
regards,
Peter
----- Original Message -----
From: "mark goldin" <ma...@gmail.com>
To: "users" <us...@flex.apache.org>
Sent: Friday, September 27, 2013 3:01 AM
Subject: Re: Send parameter to mxml constructor
> 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.
>
>
> On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
> <ke...@gmail.com>wrote:
>
>> Let me make the example a little more neutral.
>>
>>
>> class being called.
>>
>> public class MyClass inherits UIComponent
>> {
>> /**
>> * Constructor.
>> */
>> public function MyClass ():void
>> {
>> super();
>> }
>>
>> //----------------------------------------
>> // My setup property
>> //----------------------------------------
>> protected _myProperty:Object;
>>
>> public function get myProperty():Object
>> {
>> return
>> }
>>
>> public function set myProperty(value:Object):void
>> {
>> if (_myProperty == value)
>> {
>> return;
>> }
>>
>> _myProperty = value;
>>
>> setupChildren();
>> }
>>
>> protected function setupChildren():void
>> {
>> //using the _myProperty to setup the children and marking
>> anything as being ready and available... events n such.
>> }
>> }
>>
>>
>> mxml side
>>
>> <ns:MyClass myProperty="The property i'm passing to initialize" />
>>
>>
>>
>>
>> -Mark
>>
>>
>>
>> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
>> wrote:
>>
>> > Are you saying I have to involve the DI framework?
>> >
>> >
>> > On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>> > mark.kessler.ctr@usmc.mil> wrote:
>> >
>> > > Setter Dependency Injection(mxml compatible) vs constructor Dependency
>> > > Injection (AS compatible).
>> > >
>> > > -Mark
>> > >
>> > > -----Original Message-----
>> > > From: mark goldin [mailto:markzolotoy@gmail.com]
>> > > Sent: Thursday, September 26, 2013 2:57 PM
>> > > To: users
>> > > Subject: Send parameter to mxml constructor
>> > >
>> > > I have an mxml class called Obj1. Somewhere in my code I do:
>> > > var _obj1 = new Obj1();
>> > >
>> > > What I need is to provide some variable to Obj1 for its
>> initialization. A
>> > > reason for that is because Obj1 has children that need that variable
>> for
>> > > their initialization.
>> > > But because I cannot send a parameter to Obj1's constructor I dont know
>> > how
>> > > to solve my problem.
>> > > Any idea?
>> > >
>> > > Thanks
>> > >
>> >
>>
>
Re: Send parameter to mxml constructor
Posted by Jeff Gomes <jg...@mmmagic.com>.
Sure (note: untested pseudo-code, assuming Obj1 has a public String
property named "myProperty" and is in a package called "mypackage" which
has been imported):
var cf:ClassFactory = new ClassFactory(mypackage.Obj1);
cf.properties = {myProperty: "whateverThisNeedsToBe"};
var _obj1:Obj1 = cf.newInstance();
~ Jeff
On 9/26/13 6:26 PM, mark goldin wrote:
> Could you sample some code, please?
>
>
> On Thu, Sep 26, 2013 at 8:24 PM, Jeff Gomes <jg...@mmmagic.com> wrote:
>
>> Perhaps you could create a classFactory and set the property on that
>> dynamically, then use the classFactory's newInstance method to get your
>> instance.
>>
>> ~ Jeff
>>
>>
>> On 9/26/13 6:01 PM, mark goldin 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.
>>>
>>>
>>> On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
>>> <ke...@gmail.com>**wrote:
>>>
>>> Let me make the example a little more neutral.
>>>>
>>>> class being called.
>>>>
>>>> public class MyClass inherits UIComponent
>>>> {
>>>> /**
>>>> * Constructor.
>>>> */
>>>> public function MyClass ():void
>>>> {
>>>> super();
>>>> }
>>>>
>>>> //----------------------------**------------
>>>> // My setup property
>>>> //----------------------------**------------
>>>> protected _myProperty:Object;
>>>>
>>>> public function get myProperty():Object
>>>> {
>>>> return
>>>> }
>>>>
>>>> public function set myProperty(value:Object):void
>>>> {
>>>> if (_myProperty == value)
>>>> {
>>>> return;
>>>> }
>>>>
>>>> _myProperty = value;
>>>>
>>>> setupChildren();
>>>> }
>>>>
>>>> protected function setupChildren():void
>>>> {
>>>> //using the _myProperty to setup the children and marking
>>>> anything as being ready and available... events n such.
>>>> }
>>>> }
>>>>
>>>>
>>>> mxml side
>>>>
>>>> <ns:MyClass myProperty="The property i'm passing to initialize" />
>>>>
>>>>
>>>>
>>>>
>>>> -Mark
>>>>
>>>>
>>>>
>>>> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
>>>> wrote:
>>>>
>>>> Are you saying I have to involve the DI framework?
>>>>>
>>>>> On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>>>>> mark.kessler.ctr@usmc.mil> wrote:
>>>>>
>>>>> Setter Dependency Injection(mxml compatible) vs constructor Dependency
>>>>>> Injection (AS compatible).
>>>>>>
>>>>>> -Mark
>>>>>>
>>>>>> -----Original Message-----
>>>>>> From: mark goldin [mailto:markzolotoy@gmail.com]
>>>>>> Sent: Thursday, September 26, 2013 2:57 PM
>>>>>> To: users
>>>>>> Subject: Send parameter to mxml constructor
>>>>>>
>>>>>> I have an mxml class called Obj1. Somewhere in my code I do:
>>>>>> var _obj1 = new Obj1();
>>>>>>
>>>>>> What I need is to provide some variable to Obj1 for its
>>>>>>
>>>>> initialization. A
>>>>> reason for that is because Obj1 has children that need that variable
>>>>> for
>>>>> their initialization.
>>>>>> But because I cannot send a parameter to Obj1's constructor I dont know
>>>>>>
>>>>> how
>>>>>
>>>>>> to solve my problem.
>>>>>> Any idea?
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>>
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
Could you sample some code, please?
On Thu, Sep 26, 2013 at 8:24 PM, Jeff Gomes <jg...@mmmagic.com> wrote:
> Perhaps you could create a classFactory and set the property on that
> dynamically, then use the classFactory's newInstance method to get your
> instance.
>
> ~ Jeff
>
>
> On 9/26/13 6:01 PM, mark goldin 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.
>>
>>
>> On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
>> <ke...@gmail.com>**wrote:
>>
>> Let me make the example a little more neutral.
>>>
>>>
>>> class being called.
>>>
>>> public class MyClass inherits UIComponent
>>> {
>>> /**
>>> * Constructor.
>>> */
>>> public function MyClass ():void
>>> {
>>> super();
>>> }
>>>
>>> //----------------------------**------------
>>> // My setup property
>>> //----------------------------**------------
>>> protected _myProperty:Object;
>>>
>>> public function get myProperty():Object
>>> {
>>> return
>>> }
>>>
>>> public function set myProperty(value:Object):void
>>> {
>>> if (_myProperty == value)
>>> {
>>> return;
>>> }
>>>
>>> _myProperty = value;
>>>
>>> setupChildren();
>>> }
>>>
>>> protected function setupChildren():void
>>> {
>>> //using the _myProperty to setup the children and marking
>>> anything as being ready and available... events n such.
>>> }
>>> }
>>>
>>>
>>> mxml side
>>>
>>> <ns:MyClass myProperty="The property i'm passing to initialize" />
>>>
>>>
>>>
>>>
>>> -Mark
>>>
>>>
>>>
>>> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
>>> wrote:
>>>
>>> Are you saying I have to involve the DI framework?
>>>>
>>>>
>>>> On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>>>> mark.kessler.ctr@usmc.mil> wrote:
>>>>
>>>> Setter Dependency Injection(mxml compatible) vs constructor Dependency
>>>>> Injection (AS compatible).
>>>>>
>>>>> -Mark
>>>>>
>>>>> -----Original Message-----
>>>>> From: mark goldin [mailto:markzolotoy@gmail.com]
>>>>> Sent: Thursday, September 26, 2013 2:57 PM
>>>>> To: users
>>>>> Subject: Send parameter to mxml constructor
>>>>>
>>>>> I have an mxml class called Obj1. Somewhere in my code I do:
>>>>> var _obj1 = new Obj1();
>>>>>
>>>>> What I need is to provide some variable to Obj1 for its
>>>>>
>>>> initialization. A
>>>
>>>> reason for that is because Obj1 has children that need that variable
>>>>>
>>>> for
>>>
>>>> their initialization.
>>>>> But because I cannot send a parameter to Obj1's constructor I dont know
>>>>>
>>>> how
>>>>
>>>>> to solve my problem.
>>>>> Any idea?
>>>>>
>>>>> Thanks
>>>>>
>>>>>
>
Re: Send parameter to mxml constructor
Posted by Jeff Gomes <jg...@mmmagic.com>.
Perhaps you could create a classFactory and set the property on that
dynamically, then use the classFactory's newInstance method to get your
instance.
~ Jeff
On 9/26/13 6:01 PM, mark goldin 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.
>
>
> On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
> <ke...@gmail.com>wrote:
>
>> Let me make the example a little more neutral.
>>
>>
>> class being called.
>>
>> public class MyClass inherits UIComponent
>> {
>> /**
>> * Constructor.
>> */
>> public function MyClass ():void
>> {
>> super();
>> }
>>
>> //----------------------------------------
>> // My setup property
>> //----------------------------------------
>> protected _myProperty:Object;
>>
>> public function get myProperty():Object
>> {
>> return
>> }
>>
>> public function set myProperty(value:Object):void
>> {
>> if (_myProperty == value)
>> {
>> return;
>> }
>>
>> _myProperty = value;
>>
>> setupChildren();
>> }
>>
>> protected function setupChildren():void
>> {
>> //using the _myProperty to setup the children and marking
>> anything as being ready and available... events n such.
>> }
>> }
>>
>>
>> mxml side
>>
>> <ns:MyClass myProperty="The property i'm passing to initialize" />
>>
>>
>>
>>
>> -Mark
>>
>>
>>
>> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
>> wrote:
>>
>>> Are you saying I have to involve the DI framework?
>>>
>>>
>>> On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>>> mark.kessler.ctr@usmc.mil> wrote:
>>>
>>>> Setter Dependency Injection(mxml compatible) vs constructor Dependency
>>>> Injection (AS compatible).
>>>>
>>>> -Mark
>>>>
>>>> -----Original Message-----
>>>> From: mark goldin [mailto:markzolotoy@gmail.com]
>>>> Sent: Thursday, September 26, 2013 2:57 PM
>>>> To: users
>>>> Subject: Send parameter to mxml constructor
>>>>
>>>> I have an mxml class called Obj1. Somewhere in my code I do:
>>>> var _obj1 = new Obj1();
>>>>
>>>> What I need is to provide some variable to Obj1 for its
>> initialization. A
>>>> reason for that is because Obj1 has children that need that variable
>> for
>>>> their initialization.
>>>> But because I cannot send a parameter to Obj1's constructor I dont know
>>> how
>>>> to solve my problem.
>>>> Any idea?
>>>>
>>>> Thanks
>>>>
Re: Send parameter to mxml constructor
Posted by Mihai Chira <mi...@gmail.com>.
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 <ah...@adobe.com> wrote:
>
>
> On 9/27/13 8:38 AM, "mark goldin" <ma...@gmail.com> 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 <ah...@adobe.com> 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" <ma...@gmail.com> 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
> >> ><ke...@gmail.com>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 <ma...@gmail.com>
> >> >> 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.
> >> >> >
> >> >> >
> >> >>
> >>
> >>
>
>
RE: Send parameter to mxml constructor
Posted by Kessler CTR Mark J <ma...@usmc.mil>.
Could always use the events I like initialize on the component.
-----Original Message-----
From: mark goldin [mailto:markzolotoy@gmail.com]
Sent: Friday, September 27, 2013 11:39 AM
To: users
Subject: Re: Send parameter to mxml constructor
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? Is this the sample you are referring to:
Re: Send parameter to mxml constructor
Posted by Alex Harui <ah...@adobe.com>.
On 9/27/13 8:38 AM, "mark goldin" <ma...@gmail.com> 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 <ah...@adobe.com> 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" <ma...@gmail.com> 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
>> ><ke...@gmail.com>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 <ma...@gmail.com>
>> >> 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.
>> >> >
>> >> >
>> >>
>>
>>
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
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? Is this the sample you are referring to:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/ObjectComp.mxml -->
<fx:Object xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
implements="mx.core.IMXMLObject">
<fx:Script>
<![CDATA[
// Implement the IMXMLObject.initialized() method.
public function initialized(document:Object, id:String):void {
trace("initialized, x = " + x);
}
]]>
</fx:Script>
<fx:Declarations>
<fx:Number id="y"/>
<fx:Number id="z"/>
<fx:Number id="x"/>
</fx:Declarations>
</fx:Object>
On Thu, Sep 26, 2013 at 10:24 PM, Alex Harui <ah...@adobe.com> 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" <ma...@gmail.com> 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
> ><ke...@gmail.com>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 <ma...@gmail.com>
> >> 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.
> >> >
> >> >
> >>
>
>
Re: Send parameter to mxml constructor
Posted by Alex Harui <ah...@adobe.com>.
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" <ma...@gmail.com> 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
><ke...@gmail.com>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 <ma...@gmail.com>
>> 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.
>> >
>> >
>>
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
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
<ke...@gmail.com>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 <ma...@gmail.com>
> 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.
> >
> >
>
Re: Send parameter to mxml constructor
Posted by Mark Kessler <ke...@gmail.com>.
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 <ma...@gmail.com> 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.
>
>
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
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.
On Thu, Sep 26, 2013 at 7:17 PM, Mark Kessler
<ke...@gmail.com>wrote:
> Let me make the example a little more neutral.
>
>
> class being called.
>
> public class MyClass inherits UIComponent
> {
> /**
> * Constructor.
> */
> public function MyClass ():void
> {
> super();
> }
>
> //----------------------------------------
> // My setup property
> //----------------------------------------
> protected _myProperty:Object;
>
> public function get myProperty():Object
> {
> return
> }
>
> public function set myProperty(value:Object):void
> {
> if (_myProperty == value)
> {
> return;
> }
>
> _myProperty = value;
>
> setupChildren();
> }
>
> protected function setupChildren():void
> {
> //using the _myProperty to setup the children and marking
> anything as being ready and available... events n such.
> }
> }
>
>
> mxml side
>
> <ns:MyClass myProperty="The property i'm passing to initialize" />
>
>
>
>
> -Mark
>
>
>
> On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com>
> wrote:
>
> > Are you saying I have to involve the DI framework?
> >
> >
> > On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
> > mark.kessler.ctr@usmc.mil> wrote:
> >
> > > Setter Dependency Injection(mxml compatible) vs constructor Dependency
> > > Injection (AS compatible).
> > >
> > > -Mark
> > >
> > > -----Original Message-----
> > > From: mark goldin [mailto:markzolotoy@gmail.com]
> > > Sent: Thursday, September 26, 2013 2:57 PM
> > > To: users
> > > Subject: Send parameter to mxml constructor
> > >
> > > I have an mxml class called Obj1. Somewhere in my code I do:
> > > var _obj1 = new Obj1();
> > >
> > > What I need is to provide some variable to Obj1 for its
> initialization. A
> > > reason for that is because Obj1 has children that need that variable
> for
> > > their initialization.
> > > But because I cannot send a parameter to Obj1's constructor I dont know
> > how
> > > to solve my problem.
> > > Any idea?
> > >
> > > Thanks
> > >
> >
>
Re: Send parameter to mxml constructor
Posted by Mark Kessler <ke...@gmail.com>.
Let me make the example a little more neutral.
class being called.
public class MyClass inherits UIComponent
{
/**
* Constructor.
*/
public function MyClass ():void
{
super();
}
//----------------------------------------
// My setup property
//----------------------------------------
protected _myProperty:Object;
public function get myProperty():Object
{
return
}
public function set myProperty(value:Object):void
{
if (_myProperty == value)
{
return;
}
_myProperty = value;
setupChildren();
}
protected function setupChildren():void
{
//using the _myProperty to setup the children and marking
anything as being ready and available... events n such.
}
}
mxml side
<ns:MyClass myProperty="The property i'm passing to initialize" />
-Mark
On Thu, Sep 26, 2013 at 3:18 PM, mark goldin <ma...@gmail.com> wrote:
> Are you saying I have to involve the DI framework?
>
>
> On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
> mark.kessler.ctr@usmc.mil> wrote:
>
> > Setter Dependency Injection(mxml compatible) vs constructor Dependency
> > Injection (AS compatible).
> >
> > -Mark
> >
> > -----Original Message-----
> > From: mark goldin [mailto:markzolotoy@gmail.com]
> > Sent: Thursday, September 26, 2013 2:57 PM
> > To: users
> > Subject: Send parameter to mxml constructor
> >
> > I have an mxml class called Obj1. Somewhere in my code I do:
> > var _obj1 = new Obj1();
> >
> > What I need is to provide some variable to Obj1 for its initialization. A
> > reason for that is because Obj1 has children that need that variable for
> > their initialization.
> > But because I cannot send a parameter to Obj1's constructor I dont know
> how
> > to solve my problem.
> > Any idea?
> >
> > Thanks
> >
>
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
Are you saying I have to involve the DI framework?
On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
mark.kessler.ctr@usmc.mil> wrote:
> Setter Dependency Injection(mxml compatible) vs constructor Dependency
> Injection (AS compatible).
>
> -Mark
>
> -----Original Message-----
> From: mark goldin [mailto:markzolotoy@gmail.com]
> Sent: Thursday, September 26, 2013 2:57 PM
> To: users
> Subject: Send parameter to mxml constructor
>
> I have an mxml class called Obj1. Somewhere in my code I do:
> var _obj1 = new Obj1();
>
> What I need is to provide some variable to Obj1 for its initialization. A
> reason for that is because Obj1 has children that need that variable for
> their initialization.
> But because I cannot send a parameter to Obj1's constructor I dont know how
> to solve my problem.
> Any idea?
>
> Thanks
>
RE: Send parameter to mxml constructor
Posted by Kessler CTR Mark J <ma...@usmc.mil>.
Setter Dependency Injection(mxml compatible) vs constructor Dependency Injection (AS compatible).
-Mark
-----Original Message-----
From: mark goldin [mailto:markzolotoy@gmail.com]
Sent: Thursday, September 26, 2013 2:57 PM
To: users
Subject: Send parameter to mxml constructor
I have an mxml class called Obj1. Somewhere in my code I do:
var _obj1 = new Obj1();
What I need is to provide some variable to Obj1 for its initialization. A
reason for that is because Obj1 has children that need that variable for
their initialization.
But because I cannot send a parameter to Obj1's constructor I dont know how
to solve my problem.
Any idea?
Thanks
Re: Send parameter to mxml constructor
Posted by mark goldin <ma...@gmail.com>.
But I cannot set someProp at the design time because someValue isn't
available yet.
On Thu, Sep 26, 2013 at 2:09 PM, Gordon Smith <go...@adobe.com> wrote:
> MXML does not support constructor parameters. You need to set a property,
> as in
>
> <my:Obj1 someProp="someValue"/>
>
> Properties get set before children get created.
>
> - Gordon
>
> On 9/26/13 11:57 AM, "mark goldin" <ma...@gmail.com> wrote:
>
> >I have an mxml class called Obj1. Somewhere in my code I do:
> >var _obj1 = new Obj1();
> >
> >What I need is to provide some variable to Obj1 for its initialization. A
> >reason for that is because Obj1 has children that need that variable for
> >their initialization.
> >But because I cannot send a parameter to Obj1's constructor I dont know
> >how
> >to solve my problem.
> >Any idea?
> >
> >Thanks
>
>
Re: Send parameter to mxml constructor
Posted by Gordon Smith <go...@adobe.com>.
MXML does not support constructor parameters. You need to set a property,
as in
<my:Obj1 someProp="someValue"/>
Properties get set before children get created.
- Gordon
On 9/26/13 11:57 AM, "mark goldin" <ma...@gmail.com> wrote:
>I have an mxml class called Obj1. Somewhere in my code I do:
>var _obj1 = new Obj1();
>
>What I need is to provide some variable to Obj1 for its initialization. A
>reason for that is because Obj1 has children that need that variable for
>their initialization.
>But because I cannot send a parameter to Obj1's constructor I dont know
>how
>to solve my problem.
>Any idea?
>
>Thanks