1
votes

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?

3

3 Answers

2
votes

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.

0
votes

You need to make sure you have a reference to an instance of that component from where you are trying to call it. So in the example below you can see that i've created an instance of B inside of the constructor of class A. So now i have an instance of B. Now from that instance i can call b.someMethod()

public class A
{
   public function A()
   {
      var b : B = new B();
      b.someMethod();
   }
}

public class B
{
   public function B()
   {
   }

   public function someMethod() : void
   {

   }
}

You can think of class A as your Application.mxml and class b as your custom component. So whereever you are trying to call it you need to make sure your component has been created.

0
votes

Referencing the component in that manner requires the method to be static.

If you want to invoke a method on the component's instance, assign an ID to it and invoke it from the view where you've defined that component.

<!-- component declaration -->
<comps:MyComponent id="myComp"/>

// inside a script block
myComp.mySetter(someObj)

By the way, a "setter method" in ActionScript is one that is defined using the following syntax:

public function set myProperty(varName : String) : void { ... }

And it can be "called:" just like you are setting a property value:

myComp.mySetter = myObj;

In many other languages a "setter method" is just like any other method except for the naming convention. It's worth understanding that a true setter method in AS3 is different.