Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created September 17, 2012 12:32
Show Gist options
  • Select an option

  • Save simenbrekken/3737038 to your computer and use it in GitHub Desktop.

Select an option

Save simenbrekken/3737038 to your computer and use it in GitHub Desktop.
Backbone tag collection filtering with levenshtein
var Tags = Backbone.Collection.extend({
suggest: function(suggestion, threshold) {
return this.filter(function(tag) {
return levenshtein(tag.get('name'), suggestion) < (threshold || 3);
})
}
});
var tags = new Tags([
{name: 'Cat'},
{name: 'Dog'},
{name: 'Whale'},
{name: 'Horse'},
{name: 'Seahorse'}
)];
var filtered = tags.suggest('hrose') // => [horse, seahorse ]
this.list.render(filtered);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment