R – calling mxml setter method in custom component from actionscript

actionscript-3apache-flex

im trying to call a setter method in some an actionscript block of my custom component from an actionscript class.

I can do this fine when its the main application i want to call by using ;

Application.application.methodName();

however how can i call a method from a custom component? My component is in my components package and im trying the following

components.customComponent.methodName()

however its not happening,
any ideas?


an instance of my custom componet calls the actionscript class so i dont want to instantiate the custom class again inside the actionscript, i just want to call a setter method…

any other ideas?

Best Answer

You don't have to make the method static; doing so would likely be useless, as you'd want the method call to do something with the component's current state, I'm guessing -- use some of its data, change its appearance, etc. What you really need is an object reference.

Since Application.application resides at the top (or actually very close to the top) of the fabled Display List, you should be able to access each component by starting at that point and then traversing the Display List -- until ultimately, upon arriving at your nested component, calling its publicly defined method.

However, I must say (with the utmost respect!) that you're venturing into dangerous OO waters, here. :) The right way to do this would really be to figure out some way to pass a reference to your custom component to the ActionScript class that requires access to it -- for example, in your MXML:

<mx:Script>
    <![CDATA[

        private function this_creationComplete(event:Event):void
        {
            var yourObject:YourClass = new YourClass(yourCustomComponent);
        }

    ]]>
</mx:Script>

<components:YourCustomComponent id="yourCustomComponent" />

... and then in your ActionScript class:

public class YourClass
{
    private var componentReference:YourCustomComponent;

    public function YourClass(component:YourCustomComponent)
    {
        this.componentReference = componentReference;
    }

    private function yourMethod():void
    {
        this.componentReference.someMethodDefinedInYourComponent();
    }
}

An approach like that would probably serve you better. Does it make sense? I'll keep an eye our for comments; post back and I'll do my best to help you through it.

Related Topic