If you use emitter#on you need to remember to off() the event unless you expect the emitter to die automatically together with the code where the listener was defined. The special case here is that the event listener is defined by the emitter itself (e.g. a constructor of a special kind of collection listens to #add/#remove on itself).
While destroying the editor we also destroy many of its components by calling their destroy() methods. Those methods usually call this.stopListening() which removes all listeners added by this.listenTo( emitter, eventName ). That nicely cleans up all the listeners except those added with on().
This means that there are two things we can do:
Agree that you shouldn't use on() unless you know that the emitter will die with your object.
When calling stopListening() remove also all listeners added with on() (that will make this.on() a shorthand for this.listenTo( this )).
I'm trying to understand if the latter is at all possible, because listenTo() uses on() internally. I can't answer this straight so I'll leave this open and let you comment :).
If you use
emitter#on
you need to remember tooff()
the event unless you expect the emitter to die automatically together with the code where the listener was defined. The special case here is that the event listener is defined by the emitter itself (e.g. a constructor of a special kind of collection listens to#add
/#remove
on itself).While destroying the editor we also destroy many of its components by calling their
destroy()
methods. Those methods usually callthis.stopListening()
which removes all listeners added bythis.listenTo( emitter, eventName )
. That nicely cleans up all the listeners except those added withon()
.This means that there are two things we can do:
on()
unless you know that the emitter will die with your object.stopListening()
remove also all listeners added withon()
(that will makethis.on()
a shorthand forthis.listenTo( this )
).I'm trying to understand if the latter is at all possible, because
listenTo()
useson()
internally. I can't answer this straight so I'll leave this open and let you comment :).