在执行findQuery emberjs和ember data时取消对api的现有调用

Cancel existing calls to the api when doing findQuery emberjs and ember data

本文关键字:api 调用 取消 findQuery 执行 emberjs data ember      更新时间:2023-09-26

我有一个进行实时搜索的输入字段。它被设置为对于每个按键它都做一个存储。findQuery,它触发一个api请求。我注意到,在使用它时,我需要在执行每个新的findQuery时取消对api的挂起ajax调用。随着搜索查询的改变,最终会有大量不相关的请求。有人知道怎么做吗?

没有内置的方法可以做到这一点。ajax调用本身不会返回给您用于这种类型的使用。我建议取消查找请求,以避免产生大量无用的请求。

App.IndexController = Em.Controller.extend({
  foo:undefined,
  callsToSearch: 0,
  actualSearch: 0,
  watchFoo: function(){
    this.incrementProperty('callsToSearch');
                  //context, method,     time that must elapse before calling method
    Em.run.debounce(this, this.doSearch, 300);
  }.observes('foo'),
  doSearch: function(){
    this.incrementProperty('actualSearch');
    // do the actual search here
  }
});
http://emberjs.jsbin.com/ciyebaqo/1/edit