Backbone.js multiple delegates to view
A quick question regarding delegated objects 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 it possible?
'keyup keydown focusin focusout click .text-input': 'textEvents',
source to share
No, you cannot do this. From the exact guide :
Events are recorded in the format
{"event selector": "callback"}
event
is implicitly a single word (as in jQuery and DOM events), whereas selector
can be any jQuery style selector. Additionally, the keys in are this.events
parsed using this regex :
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
therefore it event
is the first component and only the first component.
You can create the object yourself events
and call it delegateEvents
manually as follows:
var catch = ['keyup', 'keydown', 'focusin', 'focusout', 'click'];
var events = { };
_(catch).each(function(ev) { events[ev + ' .text-input'] = 'textEvents' });
this.delegateEvents(events);
source to share