如何使用Ember.run.debounce实现正确的关闭 [英] How to achieve the correct closure using Ember.run.debounce

查看:152
本文介绍了如何使用Ember.run.debounce实现正确的关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用Ember.run.debounce时,只有当有多个子视图触发保存时,才能在父视图或控制器上触发保存操作。问题似乎是关闭(匿名函数),但是在这种情况下,我找不到在Ember中实现去抖动的最佳方法的例子。

I am trying to use Ember.run.debounce to only trigger the save action on a parent view or controller once when there are many child views triggering save. The problem seems to be closure (anonymous function), but I can't find any examples of the best way to implement debounce within Ember in this context.

这是一个jsbin概述了这个问题。任何帮助或指点赞赏!

Here is a jsbin outlining the issue. Any help or pointers appreciated!

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

推荐答案

您的怀疑是正确的,但解决方案非常简单。

Your suspicion is right but the solution is quite easy.

您的方法

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

});

我的解决方案提案:这样你就没有匿名功能并且Ember运行循环能够执行其去抖动逻辑。

My solution proposal: This way you don't have an anonymous function and the Ember run loop is able to do its debounce logic.

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

});

这篇关于如何使用Ember.run.debounce实现正确的关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆