Skip to content

Instantly share code, notes, and snippets.

@omarismail
Forked from poteto/search.js
Last active August 29, 2015 14:10
Show Gist options
  • Save omarismail/57f6905ab6e247c166b7 to your computer and use it in GitHub Desktop.
Save omarismail/57f6905ab6e247c166b7 to your computer and use it in GitHub Desktop.
// utils/computed/search.js
import Ember from 'ember';
var computed = Ember.computed;
export default function search(dependentKey, propertyKey, searchQueryKey, returnEmptyArray) {
returnEmptyArray = (typeof returnEmptyArray === "undefined") ? false : returnEmptyArray;
return computed("" + dependentKey + ".@each." + propertyKey, searchQueryKey, function() {
var items, query;
if (returnEmptyArray && !this.get(searchQueryKey)) {
return Ember.A([]);
}
query = this.get(searchQueryKey) || '';
query = query.toLowerCase();
items = this.get(dependentKey) || Ember.A([]);
return items.filter(function(item) {
if (item.get(propertyKey)) {
return item.get(propertyKey).toLowerCase().indexOf(query) !== -1;
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment