keepyourweb
keepyourweb

Reputation: 662

backbone's delegateEvents issue

I have this situation with backbone:

events: {
    'click': 'test1',
    'click .modify': 'test2'
}

When I click on .modify is fired both test1 and test2. How to solve?

http://jsfiddle.net/keepyourweb/VU8pE/1/

Upvotes: 1

Views: 503

Answers (2)

Brian Genisio
Brian Genisio

Reputation: 48137

I would have thought that e.preventDefault() would have worked too...

But, I found that e.stopImmediatePropagation() does the trick for you.

My guess is that both event callbacks have been queued by this point which is why preventDefault didn't work.

Thanks for the jsFiddle. It really helped!

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351516

The first click event is being attached to el so any clicks to .modify will bubble up to test1 as well.

If you really want to do this then set up your event handlers like this:

test1: function(e) {},

test2: function(e) {
    // this will stop "test1"
    // from being called
    e.preventDefault();
}

Upvotes: 0

Related Questions