user2952238
user2952238

Reputation: 787

Backbone js instant search collection

Im trying to implement kind of an "instant search" on a collection. I pass a string to my search function and match it with the attribute "name" in my collection. But thing is that now it needs to be a perfect match to work. eg. you don't get any results if just part of the search term matching the "name" attribute. How would i fix that?

search: function(str) {
    var models = this.collection.where({name:str});
    var search = new PeopleCollection(models)

    new PeopleView({
        el: this.$('.peoplelist'),
        collection: search
    });
},

Upvotes: 2

Views: 937

Answers (1)

Dethariel
Dethariel

Reputation: 3614

Try this approach:

var models = this.collection.filter(function(item) {
    return item.get("name").indexOf(str) > -1
});

Upvotes: 4

Related Questions