Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Created December 18, 2015 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjeurissen/2b4ba438d6ce99cfcbcf to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/2b4ba438d6ce99cfcbcf to your computer and use it in GitHub Desktop.
filter-select
import Ember from 'ember';
import FilterSelectMixin from 'applicationfilter-select';
const {RSVP} = Ember;
export default Ember.Controller.extend(FilterSelectMixin, {
search: '',
filter: '',
queryParams: ['search', 'filter'],
sortAscending: false,
sortProperties: ['createdAt'],
filteredContent: [],
i18n: Ember.inject.service(),
filters: {
featured: () => item.get('featured') === true,
my: (item) => item.get('owner') === true,
following: (item) => item.get('myWalk') !== null,
completed: (item) => item.get('completed') === true,
},
retrieveContent: Ember.observer(
'model.content.@each.title',
'model.content.@each.description',
'search',
'filter', function() {
new RSVP.Promise((resolve, reject) => {
this.filterContent(
this.get('model'), // collection to be filtered
this.get('search'), // search string
['title', 'description', 'author.name'], // attributes to search in
this.filters[this.get('filter')], // additional filter function
resolve //promise resolve function
);
}).then((filteredContent) => {
debugger;
this.set('filteredContent', filteredContent);
}, (error) => {debugger;});
}),
filterSelectArray: Ember.computed('i18n.locale', function () {
var i18n = this.get('i18n');
return Ember.A([
{ value: '', label: i18n.t('paths.filters.all') },
{ value: 'featured', label: i18n.t('paths.filters.featured') },
{ value: 'my', label: i18n.t('paths.filters.my') },
{ value: 'following', label: i18n.t('paths.filters.following') },
{ value: 'completed', label: i18n.t('paths.filters.completed') }
]);
}),
noPaths: Ember.computed('model.content.[]', function () {
return this.store.all('path').get('length') === 0;
}),
actions: {
edit(path) {
this.transitionToRoute('path.edit', path);
}
}
});
import Ember from 'ember';
export default Ember.Mixin.create({
filterSelectArray: [],
_lower: (val) => val.toLowerCase().trim(),
_checkAttr: (x, y, lower) => x && (lower(x).indexOf(lower(y)) > -1),
filterContent(collection, search, attrs=[], filterFunction, resolve) {
// don't filter the content if no searchstring is provided
if (!search) {resolve(collection);}
let content = collection;
// failsafe for when a string is passed instead of a collection of attributes
if(!Array.isArray(attrs)) {attrs = [attrs];}
// loop over all attributes and check if it contains the provided
// searchstring
content = content.filter((item) => {
let found = false;
attrs.forEach((attr) => {
if(found) {return;}
const attrFound = this._checkAttr(item.get(attr), search, this._lower);
found = found || attrFound
});
return found;
});
// if an additional filter function is provided execute it aswell before
// returning the resulting filtered content
if (filterFunction) {
content = content.filter(filterFunction);
}
debugger;
resolve(content);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{
"version": "0.4.17",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment