【问题标题】:Filtering collections in backbone过滤骨干中的集合
【发布时间】:2013-08-06 10:45:50
【问题描述】:

您好,我正在搜索骨干集合,我正在搜索的当前字符串是

项目编号

这应该返回 2 个结果,但是它只显示 1 个结果,我不明白为什么,下面是我使用的代码,

添加搜索参数时运行的代码,

updateSearchFilter: function(e) {
    var self = this;
    this.collection.each(this.filterHide, this);
    if($(e.currentTarget).val() === "")
    {
        this.collection.each(this.filterShow, this);
    }

    var activeLabels = $(".labels").find(".inactive");
    $.each(activeLabels, function(key, index){
        switch($(index).attr("id"))
        {
            case "pending":
                status = "7";
                self.filterDetails.states["pending"] = false;
            break;

            case "completed":
                status = "5";
                self.filterDetails.states["complete"] = false;
            break;

            case "archived":
                status = "3";
                self.filterDetails.states["archived"] = false;
            break;

            case "active":
                status = "1";
                self.filterDetails.states["active"] = false;
            break;
        }
    });

    var visible = this.collection.search($(e.currentTarget).val(), this.filterDetails.states);
    if (visible !== undefined) {
        visible.each(this.filterShow, this);
    }
},

所以上面的代码,在第一次按键时隐藏了单独的结果,然后循环遍历一个 jquery 对象数组并重新分配一个对象中的一些值 - 这样做是为了我们可以计算出我们需要什么过滤器搜索。

然后我们运行我们的搜索代码,

ProjectCollection.prototype.search = function(searchTerm, filters) {

  var pattern;
  console.log(filters);
  pattern = new RegExp(searchTerm, "gi");

  if (filters.active && filters.archived) {
    if (searchTerm === "") {
      return this;
    }
    return _(this.filter(function(data) {
      return pattern.test(data.get("project_name") + data.get("client_name"));
    }));


  } else if (filters.active) {
    if (searchTerm === "") {
      return this.active();
    }
    return _(this.active().filter(function(data) {
      return pattern.test(data.get("project_name") + data.get("client_name"));
    }));


  } else if (filters.pending) {
    console.log("hello");
    if (searchTerm === "") {
      return this.pending();
    }
    return _(this.pending().filter(function(data) {
      return pattern.test(data.get("project_name") + data.get("client_name"));
    }));


  } else if (filters.archived) {
    if (searchTerm === "") {
      return this.archived();
    }
    return _(this.archived().filter(function(data) {
      return pattern.test(data.get("project_name") + data.get("client_name"));
    }));


  } else if (filters.complete) {
    if (searchTerm === "") {
      return this.complete();
    }
    return _(this.complete().filter(function(data) {
      return pattern.test(data.get("project_name") + data.get("client_name"));
    }));
  }
};

ProjectCollection.prototype.archived = function() {
  return new ProjectCollection(this.where({
    status: '3'
  }));
};

ProjectCollection.prototype.active = function() {
  return new ProjectCollection(this.where({
    status: '1'
  }));
};

ProjectCollection.prototype.pending = function() {
  return new ProjectCollection(this.where({
    status: '7'
  }))
};

ProjectCollection.prototype.complete = function() {
  return new ProjectCollection(this.where({
    status: '5'
  }));
}

现在我应该得到 2 个结果,

项目编号 1 和项目编号 2

但是,我在这个搜索词中只得到一个结果,项目编号 1 的状态为“已归档”,项目编号 2 的状态为“待定”。然而,即使filters.pending = true;

我怎样才能确保我得到每个返回状态的所有匹配项?

【问题讨论】:

  • 如果您从未在控制台中看到“hello”消息,那么显然filters.active 必须在评估true。 (filters.archived 的值与未决案例是否执行无关。)由于您将filters 对象回显到控制台,因此请查看filters.active 是什么。

标签: javascript backbone.js underscore.js backbone-views backbone.js-collections


【解决方案1】:

您可以将所有过滤器状态键保存在一个数组中,而不是单独设置过滤器的状态。例如,您的用户想要过滤 activecompletepending,因此您将拥有一个 ['1','5','7'] 数组

因此,当您过滤相关状态时,您可以通过

// filterModes is ['1', '5', '7'];
collection.filter(function(model){
    return filterModes.indexOf(model.status) !== -1;
}

剩下的对你来说应该更简单了

请注意,collection.filter()collection.where() 返回数组和数组可能有也可能没有filter(),并且绝对没有where(),所以要小心链接你的函数调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多