I was wondering if it is possible to bind multiple event types in backbone within a single line.
Consider the following:
var MyView = Backbone.View.extend({
id: 'foo',
events: {
'click .bar': 'doSomething',
'touchstart .bar': 'doSomething'
},
doSomething: function(e) {
console.log(e.type);
}
});
Basically what I am wondering is if it is possible to combine the event binding for 'click' and 'touchstart' into one line - along the lines of:
events: { 'click,touchstart .bar': 'doSomething' }
Any suggestions would be appreciated.
For anyone interested I ended up overriding
delegateEvents
in Backbone.View.There are only a few modified lines to get the desired functionality.
You can see a diff in my commit on github
Here is
delegateEvents
in its modified state:It's impossible for views jQuery events, which are bound through
delegateEvents
. It's is possible for backbone events, though:We could also do it as below. With the advantage to manage space between each event.
Github commit here
Add it in Backbone directly :
Override delegateEvents method :
We could now manage events in one line :