-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f0e942
commit 577db36
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Ember from 'ember'; | ||
|
||
const { | ||
Helper, | ||
computed: { filter, filterBy }, | ||
defineProperty, | ||
get, | ||
isArray, | ||
isEmpty, | ||
observer, | ||
set | ||
} = Ember; | ||
|
||
export default Helper.extend({ | ||
compute([byPath, value, array]) { | ||
|
||
if (!isArray(array) && isArray(value)) { | ||
array = value; | ||
value = undefined; | ||
} | ||
|
||
set(this, 'array', array); | ||
set(this, 'byPath', byPath); | ||
set(this, 'value', value); | ||
|
||
return get(this, 'content'); | ||
}, | ||
|
||
byPathDidChange: observer('byPath', 'value', function() { | ||
let byPath = get(this, 'byPath'); | ||
let value = get(this, 'value'); | ||
|
||
if (isEmpty(byPath)) { | ||
defineProperty(this, 'content', []); | ||
return; | ||
} | ||
|
||
let filterFn; | ||
|
||
if (value) { | ||
if (typeof value === 'function') { | ||
filterFn = (item) => !value(get(item, byPath)); | ||
} else { | ||
filterFn = (item) => get(item, byPath) !== value; | ||
} | ||
} else { | ||
filterFn = (item) => isEmpty(get(item, byPath)); | ||
} | ||
|
||
let cp = filter(`array.@each.${byPath}`, filterFn); | ||
|
||
defineProperty(this, 'content', cp); | ||
}), | ||
|
||
contentDidChange: observer('content', function() { | ||
this.recompute(); | ||
}) | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, rejectBy } from 'ember-composable-helpers/helpers/reject-by'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import Ember from 'ember'; | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
const { A: emberArray, run, set } = Ember; | ||
|
||
moduleForComponent('reject-by', 'Integration | Helper | {{reject-by}}', { | ||
integration: true | ||
}); | ||
|
||
test('It reject by value', function(assert) { | ||
this.set('array', emberArray([ | ||
{ foo: false, name: 'a' }, | ||
{ foo: true, name: 'b' }, | ||
{ foo: false, name: 'c' } | ||
])); | ||
|
||
this.render(hbs` | ||
{{~#each (reject-by 'foo' true array) as |item|~}} | ||
{{~item.name~}} | ||
{{~/each~}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'ac', 'b is filtered out'); | ||
}); | ||
|
||
test('It rejects by truthiness', function(assert) { | ||
this.set('array', emberArray([ | ||
{ foo: 'x', name: 'a' }, | ||
{ foo: undefined, name: 'b' }, | ||
{ foo: 1, name: 'c' }, | ||
{ foo: null, name: 'd' }, | ||
{ foo: [1, 2, 3], name: 'e' } | ||
])); | ||
|
||
this.render(hbs` | ||
{{~#each (reject-by 'foo' array) as |item|~}} | ||
{{~item.name~}} | ||
{{~/each~}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'bd', 'a, c and e are filtered out'); | ||
}); | ||
|
||
test('It recomputes the filter if array changes', function(assert) { | ||
let array = emberArray([ | ||
{ foo: false, name: 'a' }, | ||
{ foo: true, name: 'b' }, | ||
{ foo: false, name: 'c' } | ||
]); | ||
|
||
this.set('array', array); | ||
|
||
this.render(hbs` | ||
{{~#each (reject-by 'foo' true array) as |item|~}} | ||
{{~item.name~}} | ||
{{~/each~}} | ||
`); | ||
|
||
run(() => array.pushObject({ foo: false, name: 'd' })); | ||
|
||
assert.equal(this.$().text().trim(), 'acd', 'd is added'); | ||
}); | ||
|
||
test('It recomputes the filter if a value under given path changes', function(assert) { | ||
let array = emberArray([ | ||
{ foo: false, name: 'a' }, | ||
{ foo: true, name: 'b' }, | ||
{ foo: false, name: 'c' } | ||
]); | ||
|
||
this.set('array', array); | ||
|
||
this.render(hbs` | ||
{{~#each (reject-by 'foo' true array) as |item|~}} | ||
{{~item.name~}} | ||
{{~/each~}} | ||
`); | ||
|
||
run(() => set(array.objectAt(1), 'foo', false)); | ||
|
||
assert.equal(this.$().text().trim(), 'abc', 'b is shown'); | ||
}); | ||
|
||
test('It can be passed an action', function(assert) { | ||
this.set('array', emberArray([ | ||
{ foo: 1, name: 'a' }, | ||
{ foo: 2, name: 'b' }, | ||
{ foo: 3, name: 'c' } | ||
])); | ||
|
||
this.on('isEven', (value) => value % 2 === 0); | ||
|
||
this.render(hbs` | ||
{{~#each (reject-by 'foo' (action 'isEven') array) as |item|~}} | ||
{{~item.name~}} | ||
{{~/each~}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'ac', 'b is filtered out'); | ||
}); |