Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 类似谷歌的骨干收藏搜索_Backbone.js_Underscore.js - Fatal编程技术网

Backbone.js 类似谷歌的骨干收藏搜索

Backbone.js 类似谷歌的骨干收藏搜索,backbone.js,underscore.js,Backbone.js,Underscore.js,我希望能够搜索backbonejs集合中包含的模型属性。我现在就是这样做的 wherePartial: function(attrs) { // this method is really only tolerant of string values. you can't do partial // matches on objects, but you can compare a list of strings. If you send it a list

我希望能够搜索backbonejs集合中包含的模型属性。我现在就是这样做的

wherePartial: function(attrs) {
      // this method is really only tolerant of string values.  you can't do partial
      // matches on objects, but you can compare a list of strings. If you send it a list
      // of values; attrs={keyA:[1,2,3],keyB:1}, etc the code will loop through the entire
      // attrs obj and look for a match. strings are partially matched and if a list is found
      // it's expected that it contains a list of string values.  The string values should be considered
      // to be like an OR operation in a query.  Non-list items are like an AND.
        if (_.isEmpty(attrs)) return [];
        var matchFound = false;
        return this.filter(function(model) {
          // this is in the outer for loop so that a function isn't created on each iteration
          function listComparator(value, index, list){
            return model.get(key).toLowerCase().indexOf(value.toLowerCase()) >= 0;
          }
          for (var key in attrs) {
            if (_.isArray(attrs[key])){
              matchFound = _.any(attrs[key],listComparator);
              if (matchFound !== true) return false;
            } else {
              matchFound = model.get(key).toLowerCase().indexOf(attrs[key].toLowerCase()) >= 0;
              if (matchFound === false) return false;
            }
          }
          return true;
        });
      }
假设“C”是一个实例化的集合,我就是这样使用它的:

姓名:乔(绰号:乔男人绰号:乔酷绰号:乔伊)

在文本框中键入并转换为:

C.wherePartial({姓名:“乔”,昵称:[“乔的男人”,“乔酷”,“乔伊”]})

上面的方法返回所有名为joe的模型,并且在该范围内,返回任何名为joe和任何昵称的模型。它对我的用途很有效。然而,我真的很想做一个不需要key:value模式的搜索。我想在搜索框中这样做,就像在网上使用搜索引擎一样。我曾考虑过只查看每个模型上的每个属性,但当您拥有一个大型集合(160k+模型)时,这需要一段时间


过去有没有人遇到过这样的需求?如果是,你是如何解决的?我希望将搜索保持在客户端,而不使用任何对后端的ajax调用。原因是整个集合已经加载到客户端。

我想到了一种方法。在模型实例化期间将属性序列化为字符串。侦听更新并更新序列化

serializeAttr: function(){
 this.serializedAttr = "";
 var self = this;
 _.each(this.toJSON(),function(value, key, list){
   self.serializedAttr += value;
 });
}
然后我可以对缓存的值进行简单搜索:

cc.serializedAttr.toLowerCase().indexOf(“joe”)>=0