Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js multiple delegateEvents for view

Tags:

backbone.js

Quick question regarding delegateEvents in a View - can we specify multiple event bindings in a single event definition?

For example, instead of:

'keyup .text-input': 'textEvents',
'keydown .text-input': 'textEvents',
'focusin .text-input': 'textEvents',
'focusout .text-input': 'textEvents',
'click .text-input': 'textEvents',
...

Is this possible?

'keyup keydown focusin focusout click .text-input': 'textEvents',
like image 845
crawf Avatar asked Dec 13 '25 15:12

crawf


1 Answers

No, you can't do that. From the fine manual:

Events are written in the format {"event selector": "callback"}

The event is implicitly a single word (just like in jQuery and the DOM events) whereas selector can be any jQuery-style selector. Furthermore, the keys in this.events are parsed using this regex:

var delegateEventSplitter = /^(\S+)\s*(.*)$/;

so the event is the first component and only the first component.

You could build the events object yourself and call delegateEvents by hand with something like this:

var catch  = ['keyup', 'keydown', 'focusin', 'focusout', 'click'];
var events = { };
_(catch).each(function(ev) { events[ev + ' .text-input'] = 'textEvents' });
this.delegateEvents(events);
like image 196
mu is too short Avatar answered Dec 16 '25 23:12

mu is too short



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!