有没有办法从对象中删除未知的事件监听器?监听器、没有办法、象中、事件

2023-09-08 13:41:16 作者:患得患失的劫

我想有一个可重复使用的按钮,其可以被注册为许多不同的回调之一,由外部源来确定。当一个新的回调设置,我想删除旧的。我也希望能够清除回调外,在任何时候。

I want to have a reusable button which can be registered for one of many different callbacks, determined by an external source. When a new callback is set, I want to remove the old. I also want to be able to clear the callback externally at any time.

public function registerButtonCallback(function:Function):void
{
  clearButtonCallback();

  button.addEventListener(MouseEvent.CLICK, function, false, 0, true);
}

public function clearButtonCallback():void
{
  if (button.hasEventListener(MouseEvent.CLICK) == true)
  {
    // do something to remove that listener
  }
}

我已经看到了在这里建议使用回调中arguments.callee的,但我不希望有绑回调的功能 - 例如,我可能希望能够点击两次按钮

I've seen suggestions on here to use "arguments.callee" within the callback, but I don't want to have that functionality tied to the callback - for example, I might want to be able to click the button twice.

建议?

推荐答案

我,你只需要一个在任何特定时间的回调函数presuming。如果这是德这样,那么为什么不与它本身被称为功能,并具有该功能可设置将在按钮的Click事件相关联的一个回调函数...

I am presuming that you want only one callback function at any given time. If that's teh case then why not have a single callback function associated with the click event on the button which itself called a function and have that function be settable...

<mx:Button click="doCallback()" .../>

public var onClickFunction:Function = null;
private function doCallback():void
{
    if (onClickFunction != null)
    {
        onClickFunction(); // optionally you can pass some parameters in here if you match the signature of your callback
    }
}

您控制的消费者里面有你的按钮将设置onClickFunction具有相应的功能。事实上,你可以将其设置为经常你喜欢。

A consumer of your control which houses your button would set the onClickFunction with the appropriate function. In fact you could set it as often as you liked.

如果你想更进一步,你可以继承的AS3 Button类和包装所有的这里面吧。

If you wanted to go one step further you could subclass the AS3 Button class and wrap all of this inside it.