문제

var ViewA = Backbone.View.extend({
        events: { 'click a': 'do_something' },
        do_something: function() { console.log('ViewA::do_something()'); }
});

var ViewB = Backbone.View.extend({
        events: { 'click a.some_class': 'do_something' },
        do_something: function() { console.log('ViewB::do_something()'); }
});

////////

<a class="some_class">Click me</a>

I've got 2 views setup that binds to click events as shown above. For some reason, ViewA's handler always gets called first.

I've tried:

  • Changing the order that the JS is loaded.
  • Changing the order that the views are initialized.

This leads me to believe that is is down to the specificity of the selectors, but that doesn't make sense either given that the selector in ViewB is more specific than the one in ViewA - why would the more general selector get called first?

도움이 되었습니까?

해결책

OK I figured this out - for anyone that runs into this in the future. The ordering of the events are determined by (in order of importance):

  1. The specificity of the "el" element of the view
  2. The specificity of the event selector
  3. The order the views are initialized
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top