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
<[email protected]>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" <[email protected]>
> To: "users" <[email protected]>
> 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
>> <[email protected]>**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 <[email protected]>
>>> wrote:
>>>
>>> > Are you saying I have to involve the DI framework?
>>> >
>>> >
>>> > On Thu, Sep 26, 2013 at 2:13 PM, Kessler CTR Mark J <
>>> > [email protected]> wrote:
>>> >
>>> > > Setter Dependency Injection(mxml compatible) vs constructor
>>> Dependency
>>> > > Injection (AS compatible).
>>> > >
>>> > > -Mark
>>> > >
>>> > > -----Original Message-----
>>> > > From: mark goldin [mailto:[email protected]]
>>> > > 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
>>> > >
>>> >
>>>
>>>
>>
>

Reply via email to