English 中文(简体)
探索主干收集
原标题:google like search of backbone collection

我愿能够寻找主干收集中所包含的模型属性。 这就是我现在怎么做。

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”是一种即时收集,这就是我如何使用:

名称:joe (nick:joe the man nickname:joechen nickname:joey)

a. 属于案文箱,并改为:

C. 部分(名称:“joe”,nicknames:[“joe the man”,“joehill”,“joey”]}

上述方法回收了所有具有名号的模型,并在这一范围内,包括任何有名号的模型和任何镍。 这对我所利用的东西来说是好的。 然而,我真的要进行不需要关键值模式的搜索。 在网上使用搜索引擎时,我愿意这样做。 我只想看一下每个模式的每个特征,但当你们有一大批收集(160k+模型)时,就算如此。

是否有人像过去那样需要? 如果是,你如何解决? 我愿对客户进行搜查,不要使用任何警示电话。 其原因是,整个收集工作已经装上客户。

问题回答

I thought of a way to do it. Serialize the attributes to a string during model instantiation. Listen for updates and update the serialization.

serializeAttr: function(){
 this.serializedAttr = "";
 var self = this;
 _.each(this.toJSON(),function(value, key, list){
   self.serializedAttr += value;
 });
}

然后,我可以简单地搜索这一附带价值:

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





相关问题
underscore.js unbind

I would love to unbind this: $("body").mousemove(_.bind(this.mousemove, this)); Due to a complicated mix between backbone.js and raphael.js I need to do the bind via underscore.js: var NodeView = ...

I think I am not using Backbone JS s views correctly?

I ve just started using Backbone (and Underscore) JS. We are doing a big iPad HTML5 application and it needs to work all client side. The project needs structure and Backbone seems to be a good fit. ...

Underscore.js: how to chain custom functions

Using Underscore.js, I can write the following which returns 42: _([42, 43]).chain() .first() .value() I have custom function, not part of Underscore.js called double(): function double(...

如何阐明基于方案拟定的 Java版。

在聊天时,我先与点子一起工作(我知道,这是原来的,但我认为这是很好的学习项目)。 强调js提供了许多功能性方案拟订概念,......

热门标签