Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a like filter with ember.js

Tags:

ember.js

I have a simple ArrayController in ember pre 1.0 and found that I can chop the list down if the filter finds an exact match for a given property, but what I can't seem to find is how do a "like" query with filter.

What I have below works if I search an array with users...

filtered = ['id', 'username'].map(function(property) {
  return self.get('content').filterProperty(property, filter);
});

... and a few of the users have the same username. For example => if I search / filter by "smith" it will return both records as the "username" property has the exact match for "smith"

How can I change this map function to work with the like style query so when I type the word "sm" it still finds both of these records

Here is the jsfiddle showing the filter I show above in action http://jsfiddle.net/Rf3h8/

Thank you in advance

like image 347
Toran Billups Avatar asked Dec 20 '22 16:12

Toran Billups


1 Answers

You can use a RegExp object to test pieces of data for a match. Since you are writing your own filter logic, you'll have to use the filter function. I've updated your fiddle to make this work: http://jsfiddle.net/Rf3h8/1/

Your fiddle contains a lot of code and may be hard for others to follow. Here is a very simple example of using RegExp to filter an array.

var names = ['ryan', 'toran', 'steve', 'test'];
var regex = new RegExp('ry');

var filtered = names.filter(function(person) {
  return regex.test(person);
});

filtered // => ['ryan']

In fact you could even refactor this to be

var filtered = names.filter(regex.test, regex);
like image 138
Ryan Avatar answered Dec 29 '22 08:12

Ryan