Ember.js 1.2.0 debounce在jquery事件上未按预期工作



我正试图在窗口的调整大小事件上设置一个反跳,并且以前能够使用下划线和jquery,如下所示:

$(window).on('resize', _.debounce($.proxy(this.windowWidthChanged, this), 333));

我接受了这个想法,并试图将其应用于Ember的Ember.run.debounce,如下所示:

$(window).on('resize', Ember.run.debounce(this, this.windowWidthChanged, 333));

事件侦听器似乎根本没有启动。。。

正如您可能已经猜到的那样,您没有将函数传递到resize事件中,而是将取消信息(调用debounce的结果)传递到ressize事件中。

var self = this;
$(window).on('resize', function(){ Ember.run.debounce(self, self.windowWidthChanged, 333)});

这又回到了经典的setTimeout困境,为什么它会立即运行?

setTimeout(alert('hello'),2000)

我最终将其封装在代理内的一个匿名函数中,以维护this上下文:

$(window).on('resize', $.proxy(function() {
    Ember.run.debounce(this, this.windowWidthChanged, 333);
}, this));

我想你也可以把Ember.run.debounce移到windowWidthChanged里面。

最新更新