在 google 关闭中使用 unlistenByKey 监听事件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现
goog.events.EventHandler
很有用。为了回答最初的问题,
goog.events.listen
在与单个事件类型一起使用时也会返回键。通过迭代每个事件类型,为每个事件类型调用goog.events.listen
并存储每个事件的密钥,您可以在上述实现中稍微更正确和简洁。You might find
goog.events.EventHandler
useful.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, callinggoog.events.listen
for each, and storing the key for each.