骨干网:仅在尚不存在的情况下添加事件侦听器 [英] Backbone: add event listener only if doesn't already exist

查看:103
本文介绍了骨干网:仅在尚不存在的情况下添加事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说:如何找到已经在听的事件列表?

In other words: how do I find list of events already being listened to?

我正在使用Backbone.on(...和Backbone.trigger(...)在彼此不了解的两个视图之间进行通信.但是,添加侦听器的视图实际上是一个"item-视图",因此我添加了许多侦听器,因此我想首先检查该事件是否已经被侦听.10x.

I'm using Backbone.on(... and Backbone.trigger(... to communicate between two Views that don't know about each other. However, the View that adds the listener is really an "item-view" for a collection and so I get many listeners added, and so I want to first check if that event is already being listened to. 10x.

推荐答案

Backbone.Events对象具有名为_events

例如,要检查某个事件是否已经被监听,可以在相关视图中实现一个功能:

So to check if some event is already being listened to you could for example implement a function in the view in question:

isEventListenedTo: function(eventName) {
  return (view._events) ? !!view._events[eventName] : false;
}

_events -dictionary包含每个事件名称的数组,因此您还可以检查事件被侦听的次数等.

The _events -dictionary contains arrays for each eventname so you could also check for how many times the event is listened to etc.

希望这会有所帮助!

这篇关于骨干网:仅在尚不存在的情况下添加事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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