如何使用Ember.run.debounce实现正确的闭包

i0n*_*i0n 3 ember.js

我试图使用Ember.run.debounce仅在有许多子视图触发保存时触发父视图或控制器上的保存操作.问题似乎是闭包(匿名函数),但我找不到任何在这个上下文中在Ember中实现去抖动的最佳方法的例子.

这是一个概述问题的jsbin.任何帮助或指针赞赏!

http://jsbin.com/esoNobo/1/edit?html,js,console,output

mav*_*ein 16

你的怀疑是正确的,但解决方案很容易.

你的方法:

App.GroupsView = Ember.View.extend({
  templateName: 'groups_view',
  actions: {
    save: function () {
      Ember.run.debounce(this, function() {
        console.log('groups view save');
        this.get('controller').send('save');
      }, 1000);
    }
  }

});
Run Code Online (Sandbox Code Playgroud)

我的解决方案建议:这样你就没有匿名函数,Ember运行循环就能够执行它的去抖动逻辑.

App.GroupsView = Ember.View.extend({
  templateName: 'groups_view',
  actions: {
    save: function () {
      Ember.run.debounce(this, this.saveFn, 1000);
    }
  },
  saveFn : function(){
    console.log('groups view save');
    this.get('controller').send('save');
  }

});
Run Code Online (Sandbox Code Playgroud)