开发者

Error when calling a function from main in flex

开发者 https://www.devze.com 2023-04-12 04:45 出处:网络
Hi guys i am trying to call a function which is defined in main.mxml public function btnAcceptCall_clickHandler(event:MouseEvent)

Hi guys i am trying to call a function which is defined in main.mxml

   public function btnAcceptCall_clickHandler(event:MouseEvent)
    {
     .....
     }

now i have a component mxml which calls this function

in this mxml i have defined a function

private function addNewCaller(event:MouseEvent):void
        {
           mx.managers.PopUpManager.removePopUp(th开发者_如何学运维is);
           Main.btnCallAndProfile_clickHandler(event)
        }

The problem is it gives an error

Call to a possibly undefined method btnCallAndProfile_clickHandler through a reference with static type Class.

anyone can point out what is the problem.??

Regards


You're coding it wrong. First off, Main.btnCallAndProfile_clickHandler is not static as mentioned in the error, nor would you want it to be static. You'll want to get an instance of Main for it to work, but for you to do that would mean that you would break good practice.

Flex is an event based language, and hence to separate concerns, you can use events to do the work for you. In this case, in Main, you would add an event listener (like say in the creationComplete event handler):

private function onCreationComplete():void
{
   addEventListener('callAndProfile', btnCallAndProfile_clickHandler);
}

And then from your addNewCaller function, you would do:

private function addNewCaller(event:MouseEvent):void
{
  dispatchEvent(new Event('callAndProfile', true));
}


J_A_X answer is pretty good, but there's another way that can still be considered clean code.

Define an interface:

interface AcceptCallHandler
{
    function acceptCall(e:MouseEvent):void;
}

Then you can implement this interface in your main.mxml and inject an AcceptCallHandler-instance into your component:

<s:Application implements="com.package.handlers.AcceptCallHandler" ...>
    <fx:Script>
        <![CDATA[
            public function acceptCall(e:MouseEvent):void
            {
                //...
            }
        ]]>
    </fx:Script>

    <component:SomeComponent acceptCallHandlerImpl="{this}"/>
</s:Application>

All you need to do in SomeComponent is:

class SomeComponent
{
    private var _acceptCallHandler:AcceptCallHandler;
    public function set acceptCallHandlerImpl(value:AcceptCallHandler):void
    {
        _acceptCallHandler = value;
    }

    private function addNewCaller(event:MouseEvent):void
    {
        mx.managers.PopUpManager.removePopUp(this);
        _acceptCallHandler.acceptCall(event);
    }
}

Using the events mechanism (and the Observer-design pattern) is not the only tool to promote loose coupling. In the above sample I'm demonstrating another 2 powerful techniques - programming to interfaces and dependency injection.


If main.mxml is you default application. You can do this:

Application.application.btnCallAndProfile_clickHandler(event);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号