我正在尝试使用Ember.run.debounce仅在有许多触发保存的子视图时触发父视图或控制器上的保存操作。问题似乎是封闭(匿名函数),但我找不到在这种情况下在Ember中实现去抖动的最佳方法的任何例子。
这是一个概述问题的jsbin。任何帮助或指示赞赏!
答案 0 :(得分: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);
}
}
});
我的解决方案建议:这样您就没有匿名功能,而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');
}
});