Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop event propagation with Backbone.js?

Using a Backbone.js View, say I want to include the following events:

    events: {         'click a': 'link',          'click': 'openPanel'      } 

How can I avoid openPanel to be fired when I click on a link. What I want is to have a clickable box which will trigger an action, but this box can have elements which should trigger other actions, and not the parent action. Think for example Twitter.com, and links in Tweets/right hand panel.

like image 778
Gunnar Lium Avatar asked Mar 28 '11 10:03

Gunnar Lium


People also ask

How do you stop event propagation?

To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.

How do you stop event propagation in typescript?

Try with (click)="$event. stopPropagation()" .

How does an event propagation in Javascript?

In the capturing phase, events propagate from the Window down through the DOM tree to the target node. For example, if the user clicks a hyperlink, that click event would pass through the <html> element, the <body> element, and the <p> element containing the link.

Which is the purpose of stopPropagation event?

stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.


2 Answers

I've been using e.stopImmediatePropagation(); in order to keep the event from propagating. I wish there was a shorter way to do this. I would like return false; but that is due to my familiarity with jQuery

like image 75
Tim Banks Avatar answered Oct 16 '22 08:10

Tim Banks


The JQuery preventDefault method would also be a good option.

    window.LocationViewLI = Backbone.View.extend({         tagName: "li",         template: _.template('<a href="/locations/<%= id %>"><%= name %></a>'),          events: {             "click a": "handleClick"         },               handleClick: function(event) {             event.preventDefault();             console.log("LocationViewLI handleClick", this.model.escape("name") );             // debugger;         },         ... 
like image 32
jspooner Avatar answered Oct 16 '22 06:10

jspooner