Binding multiple event types in backbone views

2020-05-31 06:33发布

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.

3条回答
太酷不给撩
2楼-- · 2020-05-31 07:16

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:

delegateEvents: function(events) {
    if (!(events || (events = getValue(this, 'events')))) return;
    this.undelegateEvents();
    for (var key in events) {
        var method = events[key];
        if (!_.isFunction(method)) method = this[events[key]];
        if (!method) throw new Error('Method "' + events[key] + '" does not exist');
        var match = key.match(delegateEventSplitter);
        var eventTypes = match[1].split(','), selector = match[2];
        method = _.bind(method, this);
        var self = this;
        _(eventTypes).each(function(eventName) {
            eventName += '.delegateEvents' + self.cid;
            if (selector === '') {
              self.$el.bind(eventName, method);
            } else {
                self.$el.delegate(selector, eventName, method);
            }
        });
    }
}
查看更多
萌系小妹纸
3楼-- · 2020-05-31 07:22

It's impossible for views jQuery events, which are bound through delegateEvents. It's is possible for backbone events, though:

book.on("change:title change:author", ...);
查看更多
走好不送
4楼-- · 2020-05-31 07:28

We could also do it as below. With the advantage to manage space between each event.

Github commit here

Add it in Backbone directly :

delegateEvents: function(events) {
  events || (events = _.result(this, 'events'));
  if (!events) return this;
  this.undelegateEvents();
  for (var key in events) {
    var method = events[key];
    if (!_.isFunction(method)) method = this[method];
    if (!method) continue;
    var match = key.match(delegateEventSplitter);
    this.delegate(match[1], match[2], _.bind(method, this));
  }
  return this;
}

Override delegateEvents method :

Backbone.View.prototype.originalDelegateEvents = Backbone.View.prototype.delegateEvents;
Backbone.View.prototype.delegateEvents = function(events) {
    events || (events = _.result(this, 'events'));
    if (!events) return this;
    this.undelegateEvents();
    for (var key in events) {
        var method = events[key], combinedEvents = key.split(',');
        if (!_.isFunction(method)) method = this[method];
        if (!method) continue;

        for(var i = 0, match = null; i < combinedEvents.length; ++i) {
            match = combinedEvents[i].trim().match(/^(\S+)\s*(.*)$/);
            this.delegate(match[1], match[2], _.bind(method, this));
        }
    }
    return this;
};

We could now manage events in one line :

events: { 
    'click a[data-anchor], wheel, keydown': 'scroll'
}
查看更多
登录 后发表回答