结合骨干视图的多个事件类型

结合骨干视图的多个事件类型

本文介绍了结合骨干视图的多个事件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能在骨干一行中绑定多个事件类型。

I was wondering if it is possible to bind multiple event types in backbone within a single line.

考虑以下几点:

var MyView = Backbone.View.extend({
    id: 'foo',
    events: {
        'click .bar': 'doSomething',
        'touchstart .bar': 'doSomething'
    },
    doSomething: function(e) {
        console.log(e.type);
    }
});

基本上我想知道是,如果有可能的情况下为点击和touchstart'成一条线组合结合 - 线沿线的:

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

任何建议将是AP preciated。

Any suggestions would be appreciated.

推荐答案

有关任何有兴趣我结束了在Backbone.View覆盖 delegateEvents

For anyone interested I ended up overriding delegateEvents in Backbone.View.

有只有几修改过的线,以获得所需的功能。

There are only a few modified lines to get the desired functionality.

您可以看到在我的commit在github

下面是 delegateEvents 在修改状态:

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

这篇关于结合骨干视图的多个事件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:16