有没有办法从对象中删除未知的事件监听器? [英] Is there a way to remove unknown event listeners from objects?

查看:179
本文介绍了有没有办法从对象中删除未知的事件监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于有没有办法从对象中删除未知的事件监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆