javascript - 使用 Ember.run.debounce 消除滚动滚动

标签 javascript ember.js

我有一个工作滚动混合(你可以在这个要点中看到)。

http://jsbin.com/wofaj/4

App.Scrolling = Em.Mixin.create({
  bindScrolling: function() {
    var onScroll;
    var _this = this;

    var scrollFunc = function(){ 
       return _this.scrolled(); 
    };

    onScroll = scrollFunc;

    $(document).bind('touchmove', onScroll);
    $(window).bind('scroll', onScroll);
  },

  unbindScrolling: function() {
    $(window).unbind('scroll');
    $(document).unbind('touchmove');
  }
});

App.ApplicationView = Ember.View.extend(App.Scrolling, {
    didInsertElement: function() {
        this.bindScrolling();
    },
    willRemoveElement: function() {
        this.unbindScrolling();
    },
    scrolled: function() {
      console.log('MyView was scrolled : ' + document.body.scrollTop);
    }
});

但我相信建议使用 Ember 运行循环(特别是 debounce - http://emberjs.com/api/classes/Ember.run.html#method_debounce )来消除这种情况。

这里的非工作代码示例:

http://jsbin.com/wofaj/5

问题代码在这里:

onScroll = Ember.run.debounce(this, scrollFunc, 200);

不幸的是,无论我使用什么上下文,我似乎都无法让它工作。

如果有一些见解,我们将不胜感激。

谢谢

克里斯

最佳答案

如果将 onScroll 分配给一个函数,它将起作用,

onScroll = function(){Ember.run.debounce(this, scrollFunc, 200);};

http://jsbin.com/qoxulomo/1

关于javascript - 使用 Ember.run.debounce 消除滚动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22158508/

相关文章:

javascript - 如何将 png Canvas 图像下载为 PDF JQuery

javascript - 模板仅在刷新页面后呈现

javascript - Mocha 通过了应该失败的测试 (ember-mocha-adapter)

ember.js - Ember.js集合迭代中的位置索引

javascript - 带字符转义的字符范围

javascript - 如何扩展正则表达式对象

javascript - AJAX 调用中的变量范围

c# - IE VS Chrome 和 Firefox 中的网络安全(错误)

javascript - Ember异步路由: how do I display a global loading indicator while waiting for data?

javascript - 如何在 Ember 中将 'POST' 类型页面加载到 iframe 中?