efarley
efarley

Reputation: 8661

Ember JS Filter model where title contains keyword

I am trying to set up a search for a book in a library. Right now I have the search working where if the title and the keyword match it will return the result, however how can I make it so if the title contains the keyword it will return the result.

For example if the user searches for "The Adventures" they will get both "The Adventures of Huckleberry Finn" and "The Adventures of Tom Sawyer"

This is my router right now which will find an exact match:

Books.BooksSearchRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.filter('book', function (book) {
            return book.get('titleSlug') == params.keyword;
        })
    },
    renderTemplate: function (controller) {
        this.render('books/index', { controller: controller });
    }
});

I have tried doing this using a javascript indexOf function, but it always returns no matches

Books.BooksSearchRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.filter('book', function (book) {
            return book.get('titleSlug').indexOf(params.keyword) > 0;
        })
    },
    renderTemplate: function (controller) {
        this.render('books/index', { controller: controller });
    }
});

Upvotes: 1

Views: 565

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

I think that you want > -1, because 0 is still a valid match.

For example:

"foo bar".indexOf("foo"); // returns 0
"foo bar".indexOf("lorem"); // returns -1

Upvotes: 1

Related Questions