Last active
December 13, 2015 19:58
-
-
Save inoperable/4966456 to your computer and use it in GitHub Desktop.
find text in backbone collection and return an array with results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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