Jason Biondo
Jason Biondo

Reputation: 834

How to stop Backbone Event Bubbling

In backbone I will often instantiate views using something like...

new mysite.some.namespace.View();

Which will then load in my view and attach events to the DOM. The problem is with removing that view and then instantiating it again. My events will often double if I load the view twice. How can I eliminate this? Is this because my el is set to a wrapper element for the app?

What's an easy solution for this?

Upvotes: 2

Views: 1230

Answers (2)

Peter Lyons
Peter Lyons

Reputation: 145994

From the comments it sounds like you have strayed off of the backbone straight and narrow path into the weeds. Proper event handling requires some care. If you post more code, we can provide more detailed suggestions, however:

  • Make sure you are calling .remove() when you are done with a view instance. All DOM event bindings will be automatically removed.
  • Don't do whacky things like having 2 view instances reference the same element (this.el).
  • Do yourself a favor and don't mess with this.el. Don't specify it. Use tagName in your extend object if needed, but a view's el should be A) unique to that view and B) detached from the DOM until something external to the view attaches it (usually a router or composite view manager).

Upvotes: 3

Techie
Techie

Reputation: 45124

You can use e.stopImmediatePropagation(); in order to keep the event from propagating.

Upvotes: 0

Related Questions