backbone delegateevents延迟所有事件 - 包装超时

时间:2015-06-26 21:16:32

标签: javascript jquery backbone.js

我正在使用Backbone,我想知道是否有办法全局延迟从视图中触发的任何函数调用?可能通过将其包装在超时?

// this is the delegate events function:
delegateEvents: function(events) {
    if (!(events || (events = _.result(this, 'events')))) return this;
    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 eventName = match[1], selector = match[2];

        // wrap method in a timeout
        var binding = function(){
            setTimeout(function(){
                console.log('fired in timeout');
                method();
            }, 1000);
        };

        binding = _.bind(binding, this);
        eventName += '.delegateEvents' + this.cid;


        if (selector === '') {
            this.$el.on(eventName, binding);
        } else {
            this.$el.on(eventName, selector, binding);
        }
    }
    return this;
}

这里的问题是事件仍然正常触发,而不是超时。如何在超时中包装所有事件? (我只关心委托能事件,比如'点击')

0 个答案:

没有答案