在搜索文本嵌套对象内(Backbone.js的集合为例) [英] Searching for text inside nested object (Backbone.js collection as example)

查看:67
本文介绍了在搜索文本嵌套对象内(Backbone.js的集合为例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Backbone.js的集合,我需要做一个fulltextsearch。我手头的工具有以下内容:

I have a backbone.js collection where I need to do a fulltextsearch on. The tools I have at hand are the following:

Backbone.js的,underscore.js,jQuery的

Backbone.js, underscore.js, jQuery

对于那些不熟悉的骨干:

For those not familiar with backbone:

一个骨干集合就是一个对象。集合里面有高配车型的数组。每个模型都有带属性的数组。我要寻找每个属性的字符串。

A backbones collection is just an object. Inside the collection there is an array with models. Each model has an array with attributes. I have to search each attribute for a string.

在code我使用的是这样的:

The code I am using for this is:

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);

我在用的有一个处理的更好办法的工具也许有?

Maybe one of the tools I am using has a better way of dealing with this ?

推荐答案

主干延伸出很多的下划线方法到收藏类,所以你可以摆脱一些这些东西。真的你可能想impliment这个集合本身作为一种方法,那么我可能会看那些键使用良好的老式循环,尤其是如果我想打破出来的。

Backbone extends a lot of the underscore methods into the Collection class so you can get rid of some of that stuff. Really you probably want to impliment this on the collection itself as a method, then I would probably look at those keys using a good old fashioned for loop, especially if I wanted to break out of it.

// 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);
});

这是说,这将永远只能从集合返回第一个匹配。你可以preFER返回结果的数组为[模型,属性]对一个实现。

That said, this would only ever return the first match from the collection. You may prefer an implementation that returns an array of results as [model, attribute] pairs.

// 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]);
  });
});

或者,如果你想要哪个匹配,但不关心哪个属性相匹配,您可以使用模型数组过滤器

// 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');
  });
});

这篇关于在搜索文本嵌套对象内(Backbone.js的集合为例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆