Anji
Anji

Reputation: 723

flex pass parameter to custom component

I have a custom component written in action script (no UI). I am instantiating this component in an mxml file which is present in a library project. The custom component has a constructor which takes one argument.

The library project is used in some other web project from where i pass a variable to this mxml file.[I have linked source of the web project to source of library.].

I have a static variable in my library project which holds the reference of the current object of the project. I need this static variable in order to use properties present in the mxml file.

I am unable to use the property sent from the web project in the constructor of the custom component, but able to use the same in some other function present in the custom component.

Please help me!

Thanks

Anji

Upvotes: 1

Views: 1236

Answers (1)

www0z0k
www0z0k

Reputation: 4434

if you are able to access the value you need from mxml:
mxml code:

<local:MyComponent varname="value"/>

AS3 code:

private var _varname:Type;
public function MyComponent(){
    //empty constructor
}
public function set varname(newVal: Type):void{
    _varname = newVal;
    //constructor code here
}

else you'll have to find the MyComponent event that is fired when the variable you need is already accessible (e.g. CREATION_COMPLETE):
AS3 code:

public function MyComponent() {
    addEventListener(FlexEvent.CREATION_COMPLETE, onCreated);
}

private function onCreated(e:FlexEvent):void {
    //access of the variable and constructor code
}

Upvotes: 2

Related Questions