javascript - 在嵌套对象中搜索文本(以 Backbone.js 集合为例)

标签 javascript jquery backbone.js full-text-search underscore.js

我有一个 backbone.js 集合,我需要在其中进行全文搜索。我手头的工具如下:

Backbone.js、underscore.js、jQuery

对于那些不熟悉 backbone 的人:

Backbone 集合只是一个对象。在集合中有一个带有模型的数组。每个模型都有一个带有属性的数组。我必须在每个属性中搜索一个字符串。

我为此使用的代码是:

query = 'some user input';

query = $.trim(query);
query = query.replace(/ /gi, '|');

var pattern = new RegExp(query, "i");

// this.collection.forEach is the same as _.each
// only it get's the models from the collection
this.collection.forEach(function(model) {
    var check = true;
    _.each(model.attributes, function(attr){
        if(pattern.test(attr) && check){
            // Do something with the matched item
            check = false;
        }
    }, this);
}, this);

也许我正在使用的工具之一有更好的方法来处理这个问题?

最佳答案

Backbone 将许多下划线方法扩展到 Collection 类中,因此您可以摆脱其中的一些东西。实际上,您可能想将其作为一种方法在集合本身上实现,然后我可能会使用一个很好的老式 for 循环来查看这些键,特别是如果我想打破它。

// in Backbone.Collection.extend
search: function( query, callback ){
  var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
  var collection = this;
  collection.each(function(model) {
      for( k in model.attributes ){
        if( model.attributes.hasOwnProperty(k) && pattern.test(model.attributes[k]) ){ 
          callback.call( collection, model, k ); 
          break; // ends the for loop.
        }
      }
  });

}

// later
collection.search('foo', function( model, attr ){
  console.log('found foo in '+model.cid+' attribute '+attr);
});

也就是说,这只会返回集合中的第一个匹配项。您可能更喜欢将结果数组作为 [model, attribute] 对返回的实现。

// in Backbone.Collection.extend
search: function( query, callback ){
  var matches = [];
  var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
  this.each(function(model) {
      for( k in model.attributes ){
        if( model.attributes.hasOwnProperty(k) && pattern.test(model.attributes[k]) ){ 
          matches.push([model, k]);
        }
      }
  });
  callback.call( this, matches );
}

// later
collection.search('foo', function( matches ){
  _.each(matches, function(match){
    console.log('found foo in '+match[0].cid+' attribute '+match[1]);
  });
});

或者,如果您想要一组匹配的模型但不关心匹配的是哪个属性,您可以使用 filter

// in Backbone.Collection.extend
search: function( query, callback ){
  var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
  callback.call( this, this.filter(function( model ){ 
    for( k in model.attributes ){ 
      if( model.attributes.hasOwnProperty(k) && pattern.test(k) ) 
        return true;
    }
  }));
}

// later
collection.search('foo', function( matches ){
  _.each(matches, function(match){
    console.log('found foo in '+match[0].cid+' somewhere');
  });
});

关于javascript - 在嵌套对象中搜索文本(以 Backbone.js 集合为例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468039/

相关文章:

javascript - 如何创建根据提示输入类型显示文本的警报弹出窗口?

javascript - 模型属性未统一命名的 Backbone 集合和模板

javascript - Backbone - 将条目添加到集合中(从集合内部)

javascript - 如何将 anchor 标记添加到字符串 Prop ?

javascript - webpack 代码分割是什么意思以及它是如何工作的?

javascript - 如何防止意外数据导致nodejs服务器致命崩溃?

jquery - 滚动时更改 css 属性

jQuery 从特定行选择文本

javascript - 为什么我的 JavaScript 阻止浏览器访问链接

javascript - Rails-backbone gem 中 EJS 和 JST 模板文件的格式