views:

1505

answers:

7

hi,

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?

A: 

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.

cliff.meyers
A: 

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.

James Hay
A: 

is there any way i can do this without having to make my method static? i want to be able access a datagrid on my page and making the method static prevents this

combi001
See my edited response. If you want to direct a follow-up question at a specific person, it's best to add a comment to it.
cliff.meyers
A: 

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?

combi001
A: 

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.

Christian Nunciato
A: 

ok cool thnaks for that,

i think i get what your saying, when instantiating my actionScript class in my custom component, pass in a reference to my custom component. Can i do this by just passing in the this keyword as a parameter? then in my actionscript class assign the this reference to an instance of my custom component type?

combi001
A: 

that worked, thanks for all the help

combi001