crawf
crawf

Reputation: 9658

Backbone.js multiple delegateEvents for view

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',

Upvotes: 3

Views: 3926

Answers (1)

mu is too short
mu is too short

Reputation: 434665

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);

Upvotes: 7

Related Questions