在 google 关闭中使用 unlistenByKey 监听事件

发布于 2025-01-02 19:00:05 字数 1917 浏览 1 评论 0原文

goog.events 提供了 unlistenByKey 方法,允许特定取消注册听众以其独特的身份。获取此密钥的唯一方法是通过侦听每种事件类型再次调用 goog.events.listen 或使用以下命令调用 goog.events.getListener ,这是否正确监听的每种事件类型?

例如

var eventKeys = new Array(2);
goog.events.listen(document, ['mousedown', 'touchstart'], this.pressStartHandler_, false, this);
eventKey[0] = goog.events.getListener(document, 'mousedown', this.pressStartHandler_, false, this);
eventKey[1] = goog.events.getListener(document, 'touchstart', this.pressStartHandler_, false, this);    

编辑

除非回答者提供更好的建议,否则我编写了以下包装器来获取密钥:

/**
 * Wrap goog.events.listen to capture all of the event keys of the requested listeners.
 * 
 * @param {(EventTarget |
 *          goog.events.EventTarget | null)} src The node to listen to events on.
 * @param {(Array |
 *          null | string)} type Event type or array of event types.
 * @param {(Object |
 *          null)} listener Callback method, or an object with a handleEvent function.
 * @param {(boolean |
 *          undefined)} opt_capt Whether to fire in capture phase (defaults to false).
 * @param {(Object |
 *          null | undefined)} opt_handler Element in whose scope to call the listener.
 * @returns {Array.<number>} Array of keys for listeners
 */
wias.util.listen = function(src, type, listener, opt_capt, opt_handler)
{
  var i;
  var keys = new Array();
  goog.events.listen(src, type, listener, opt_capt, opt_handler);
  if (type instanceof Array)
  {
    for (i = 0; i < type.length; i++)
    {
      keys.push(goog.events.getListener(src, type[i], listener, opt_capt, opt_handler).key);
    }
  }
  else
  {
    keys.push(goog.events.getListener(src, type, listener, opt_capt, opt_handler).key);
  }
  return keys;
};

goog.events provides an unlistenByKey method allowing the specific deregistration of a listener by its unique identity. Is it correct that the only way to obtain this key is by calling either goog.events.listen again with each event type listened for or by calling goog.events.getListener with each event type listened for?

e.g.

var eventKeys = new Array(2);
goog.events.listen(document, ['mousedown', 'touchstart'], this.pressStartHandler_, false, this);
eventKey[0] = goog.events.getListener(document, 'mousedown', this.pressStartHandler_, false, this);
eventKey[1] = goog.events.getListener(document, 'touchstart', this.pressStartHandler_, false, this);    

EDIT

Barring better advice from an answerer, I'm wrote the following wrapper to get the keys:

/**
 * Wrap goog.events.listen to capture all of the event keys of the requested listeners.
 * 
 * @param {(EventTarget |
 *          goog.events.EventTarget | null)} src The node to listen to events on.
 * @param {(Array |
 *          null | string)} type Event type or array of event types.
 * @param {(Object |
 *          null)} listener Callback method, or an object with a handleEvent function.
 * @param {(boolean |
 *          undefined)} opt_capt Whether to fire in capture phase (defaults to false).
 * @param {(Object |
 *          null | undefined)} opt_handler Element in whose scope to call the listener.
 * @returns {Array.<number>} Array of keys for listeners
 */
wias.util.listen = function(src, type, listener, opt_capt, opt_handler)
{
  var i;
  var keys = new Array();
  goog.events.listen(src, type, listener, opt_capt, opt_handler);
  if (type instanceof Array)
  {
    for (i = 0; i < type.length; i++)
    {
      keys.push(goog.events.getListener(src, type[i], listener, opt_capt, opt_handler).key);
    }
  }
  else
  {
    keys.push(goog.events.getListener(src, type, listener, opt_capt, opt_handler).key);
  }
  return keys;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2025-01-09 19:00:05

您可能会发现 goog.events.EventHandler 很有用。

// Note that all handler callbacks receive 'this' as the opt_handler, by default.
var handler = new goog.events.EventHandler(this);
handler.listen(
    document, ['mousedown', 'touchstart'], this.pressStartHandler_, false);

// Some time later...
handler.removeAll();

为了回答最初的问题,goog.events.listen 在与单个事件类型一起使用时也会返回键。通过迭代每个事件类型,为每个事件类型调用 goog.events.listen 并存储每个事件的密钥,您可以在上述实现中稍微更正确和简洁。

wias.util.listen = function(src, type, listener, opt_capt, opt_handler) {
  var keys = new Array();
  if (type instanceof Array) {
    for (var i = 0; i < type.length; i++) {
      keys.push(goog.events.listen(src, type, listener, opt_capt, opt_handler));
    }
  } else {
    keys.push(goog.events.listen(src, type, listener, opt_capt, opt_handler));
  }
  return keys;
};

You might find goog.events.EventHandler useful.

// Note that all handler callbacks receive 'this' as the opt_handler, by default.
var handler = new goog.events.EventHandler(this);
handler.listen(
    document, ['mousedown', 'touchstart'], this.pressStartHandler_, false);

// Some time later...
handler.removeAll();

To answer the original question, goog.events.listen also returns the key when used with a single event type. You could be slightly more correct and concise in your above implementation by iterating over each event type, calling goog.events.listen for each, and storing the key for each.

wias.util.listen = function(src, type, listener, opt_capt, opt_handler) {
  var keys = new Array();
  if (type instanceof Array) {
    for (var i = 0; i < type.length; i++) {
      keys.push(goog.events.listen(src, type, listener, opt_capt, opt_handler));
    }
  } else {
    keys.push(goog.events.listen(src, type, listener, opt_capt, opt_handler));
  }
  return keys;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文