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
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.
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 ?
Backbone extends a lot of the underscore methods into the
Collectionclass 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 fashionedforloop, especially if I wanted to break out of it.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.
Or, if you wanted an array of models which matched but don’t care which attribute matched you can use
filter