Magic events not tied to the dom element

In one of my views, I have:

events: {
    'click .tab': 'doSomething',
},

then

doSomething: function(){
    ...
},

This is a repeating structure in my views, but for some reason the doSomething function is not triggered by clicking in that view.

When are items associated with an event?

Any debugging tips?

+5
source share
2 answers

fiddle link here: http://jsfiddle.net/7xRak/

Lowering the selector causes the event to bind to the root element of the view ( this.el).

if your class="tab"is a DOM views element this.el, then you should bind the event as

events : {
  'click' : 'dosomething'
}

and for the inner element in this.elfor example

<div class="tab">
     <span class="inner"></span>
</div>

,

  events : {
      'click' : 'dosomething'
      'click .inner' : 'onInnerClick'
    }

document: http://backbonejs.org/#View-delegateEvents

+10

delegateEvents .

http://documentcloud.github.com/backbone/docs/backbone.html#section-144

this.delegateEvents() render this. this.el initialize render , ?

+6

All Articles