-
Notifications
You must be signed in to change notification settings - Fork 128
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
3505800
commit ff6c91e
Showing
5 changed files
with
134 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,67 @@ | ||
import Ember from 'ember'; | ||
|
||
const { | ||
A: emberArray, | ||
Helper, | ||
computed, | ||
defineProperty, | ||
get, | ||
getProperties, | ||
guidFor, | ||
isArray, | ||
isEmpty, | ||
observer, | ||
set | ||
} = Ember; | ||
|
||
const idForArray = (array) => `__array-${guidFor(array)}`; | ||
|
||
export function append(...dependentKeys) { | ||
dependentKeys = dependentKeys || []; | ||
let arrayKeys = dependentKeys.map((dependentKey) => { | ||
return `${dependentKey}.[]`; | ||
}); | ||
return computed(...arrayKeys, function() { | ||
let array = dependentKeys.map((dependentKey) => { | ||
let value = get(this, dependentKey); | ||
return isArray(value) ? value.toArray() : [value]; | ||
}); | ||
|
||
return [].concat(...array); | ||
}); | ||
}; | ||
|
||
export default Helper.extend({ | ||
compute([...arrays]) { | ||
set(this, 'arrays', arrays); | ||
|
||
return get(this, 'content'); | ||
}, | ||
|
||
valuesDidChange: observer('arrays.[]', function() { | ||
let arrays = get(this, 'arrays'); | ||
|
||
let oldArrayKeys = get(this, 'arrayKeys') || []; | ||
let newArrayKeys = arrays.map(idForArray); | ||
|
||
let keysToRemove = oldArrayKeys.filter((key) => { | ||
return newArrayKeys.indexOf(key) === -1; | ||
}); | ||
|
||
keysToRemove.forEach((key) => set(this, key, null)); | ||
arrays.forEach((array) => set(this, idForArray(array), array)); | ||
|
||
set(this, 'arrayKeys', newArrayKeys); | ||
|
||
if (isEmpty(arrays)) { | ||
defineProperty(this, 'content', []); | ||
return; | ||
} | ||
|
||
defineProperty(this, 'content', append(...newArrayKeys)); | ||
}), | ||
|
||
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, append } from 'ember-composable-helpers/helpers/append'; |
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,54 @@ | ||
import Ember from 'ember'; | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
const { A: emberArray, run } = Ember; | ||
|
||
moduleForComponent('append', 'Integration | Helper | {{append}}', { | ||
integration: true | ||
}); | ||
|
||
test('It concats two arrays', function(assert) { | ||
this.set('evens', emberArray([2, 4, 6])); | ||
this.set('odds', emberArray([1, 3, 5])); | ||
|
||
this.render(hbs` | ||
{{~#each (append evens odds) as |n|~}} | ||
{{n}} | ||
{{~/each~}} | ||
`); | ||
|
||
let expected = '246135'; | ||
|
||
assert.equal(this.$().text().trim(), expected, 'appends values'); | ||
}); | ||
|
||
test('It concats two arrays and a value', function(assert) { | ||
this.set('evens', emberArray([4, 6])); | ||
this.set('odds', emberArray([1, 3, 5])); | ||
this.set('prime', 2); | ||
|
||
this.render(hbs` | ||
{{~#each (append evens odds prime) as |n|~}} | ||
{{n}} | ||
{{~/each~}} | ||
`); | ||
|
||
let expected = '461352'; | ||
|
||
assert.equal(this.$().text().trim(), expected, 'appends values'); | ||
}); | ||
|
||
test('It watches for changes', function(assert) { | ||
this.set('odds', emberArray([1, 3, 5])); | ||
this.set('prime', 2); | ||
|
||
this.render(hbs` | ||
{{~#each (append odds prime) as |n|~}} | ||
{{n}} | ||
{{~/each~}} | ||
`); | ||
|
||
run(() => this.get('odds').pushObject(7)); | ||
assert.equal(this.$().text().trim(), '13572', 'new value is added'); | ||
}); |