Backbone: add event listener only if doesn't already exist

一曲冷凌霜 提交于 2019-12-23 10:56:25

问题


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

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.


回答1:


The Backbone.Events object has a dictionary of events called _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;
}

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

Hope this helps!




回答2:


the properties of

Object._callbacks

match your event namesw



来源:https://stackoverflow.com/questions/14377856/backbone-add-event-listener-only-if-doesnt-already-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!