Skip to content

Instantly share code, notes, and snippets.

@inoperable
Last active December 13, 2015 19:58
Show Gist options
  • Save inoperable/4966456 to your computer and use it in GitHub Desktop.
Save inoperable/4966456 to your computer and use it in GitHub Desktop.
find text in backbone collection and return an array with results
findText : function (text) {
if (text !== '' && text.length > 0) {
var result = [];
var input = text.replace(/ /g, "|");
var pattern = "(" + input + ")\\w+\\b" + "|" + "\\b(" + input + ")\\b";
pattern = new RegExp(pattern, 'gi');
this.each(function (model) {
for (var key in model.attributes) {
if (key !== '_id' && key !== '__v') {
var attr = model.attributes[key];
if (attr instanceof Array) {
attr = attr.join(' ');
}
if (attr.length > 0) {
var match = attr.match(pattern);
if (match !== null) {
result.push({
cid : model.cid,
attribute : key,
input : input,
match : match
});
}
}
}
}
});
return result;
} else {
return false;
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment