Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active January 24, 2017 19:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save poteto/30729748dba31c1af381 to your computer and use it in GitHub Desktop.
Save poteto/30729748dba31c1af381 to your computer and use it in GitHub Desktop.
simple text search computed property macro
// 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