How to make a similar filter with ember.js

I have a simple ArrayController in ember pre 1.0 and found that I can slice the list down if the filter finds an exact match for this property, but what I cannot find is how to make a “similar” request with the filter.

What i have below if i am looking for an array with users ...

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

... and multiple users have the same username. For example => if I search / filter "blacksmith", it will return both records, since the "username" property has an exact match for "smith"

How can I change this map function to work with a similar style request, so when I type the word "cm", it still finds both of these entries

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

Thank you in advance

+4
source share
1 answer

You can use the RegExp object to check for pieces of data for consistency. As you write your own filter logic, you will have to use the filter function. I updated your script to make this work: http://jsfiddle.net/Rf3h8/1/

Your violin contains a lot of code and can be difficult for others. 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 can even reorganize it as

 var filtered = names.filter(regex.test, regex); 
+10
source

Source: https://habr.com/ru/post/1435782/


All Articles