如何拒绝Ember-data PromiseArray?

时间:2014-07-17 20:46:34

标签: javascript ember.js ember-data ember-cli

我有一个拥有搜索和过滤功能的几千人的列表。搜索框在您键入时正在进行Google搜索,并且有一个下拉列表可根据此人的状态进行过滤。如果选择下拉菜单并快速开始输入,有时结果不会以相同的顺序返回,而最后一个返回的结果将在没有状态过滤器或没有搜索的情况下呈现。

我想拒绝之前的承诺,如果在任何时候触发新搜索仍然等待它。问题是,最后一次搜索被存储为PromiseArray,我可以将其称为拒绝,但它似乎并没有真正拒绝承诺。

我在ember-cli上使用ember 1.5.1和ember-data 1.0.0.beta.7.0.28

以下是生成的人物搜索控制器:

Controller = Em.ArrayController.extend({
  lastFetchedPage: 1,
  searchTerms: "",
  isFreshSearch: false,
  statusToFilterBy: null,

  statuses: (Em.computed(function() {
    return this.get("store").find("status");
  })).property(),

  statusToFilterByDidChange: (function() {
    return this.conductSearch();
  }).observes("statusToFilterBy"),

  searchTermsDidChange: (function() {
    this.haltCurrentSearch();
    this.set("searchTermsDirty", true);
    return Em.run.debounce(this, this.conductSearch, 750);
  }).observes("searchTerms"),

  conductSearch: function() {
    this.set("lastFetchedPage", 1);
    this.set("isFreshSearch", true);
    return this.fetchPeople();
  },

  haltCurrentSearch: function() {
    if (this.get("currentSearch.isPending")) {
      this.get("currentSearch").reject(new Error("Terms outdated"));
    }
  },

  fetchPeople: function() {
    var search;
    search = this.get("store").find("person-summary", {
      page: this.get("lastFetchedPage"),
      terms: this.get("searchTerms"),
      status_id: this.get("statusToFilterBy.id")
    });
    search.then((function(_this) {
      return function(personSummaries) {
        return _this.displayResults(personSummaries);
      };
    })(this));
    this.set("currentSearch", search);
    return this.set("searchTermsDirty", false);
  },

  displayResults: function(personSummaries) {
    if (this.get("isFreshSearch")) {
      this.set("isFreshSearch", false);
      return this.set("model", personSummaries);
    } else {
      return personSummaries.forEach((function(_this) {
        return function(personSummary) {
          return _this.get("model").addRecord(personSummary);
        };
      })(this));
    }
  }

  bottomVisibleChanged: function(person) {
    if (person === this.get("lastPerson")) {
      this.incrementProperty("lastFetchedPage");
      return this.fetchPeople();
    }
  },

  lastPerson: (Em.computed(function() {
    var people;
    people = this.get("model.content");
    return people[people.length - 1];
  })).property("model.@each")
});

0 个答案:

没有答案