14

我在控制器中有一个观察者,它在进行更改时保存对象。问题是它发生得太频繁了。

changed: ( ->
  #save code goes here 
).observes("description")

我在想像 http://underscorejs.org/#debounce这样的东西需要吗?

此外,当属性通过键输入更改时,似乎将对象保存两次,然后在从返回的服务器值设置属性时再次保存。

任何帮助都会很棒我正试图将我的头包裹在余烬上。

4

1 回答 1

22

从 Ember 1.0.0 开始,您可以通过将 debounce 调用包装在另一个观察的函数中,在任何 View 或 Object 中获得一个 debounce 观察者。Ember.run.debounce不返回函数,而是将函数句柄添加到字典中。随后每次使用该函数句柄调用 Ember.run.debounce 时,它​​都会检查字典以查看上次调用该函数的时间并按预期对其进行去抖动。

var MyView = Ember.View.extend({
    calledRarely: function() {
        console.log("This will log rarely.");
    },

    calledOften: function() {
        console.log("This will log often.");
        Ember.run.debounce(this, this.calledRarely, 1000);
    }.observes("propertyThatChangesOften")
});

在这里,this.calledOften它根本没有去抖动,因此Ember.run.debounce实际上会在属性更改时被调用。this.calledRarely在我们的去抖超时完成之前它不会调用。

于 2013-10-09T04:47:14.320 回答