Last active
August 15, 2024 15:15
-
-
Save poteto/30729748dba31c1af381 to your computer and use it in GitHub Desktop.
simple text search computed property macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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