addon/macros/concat.js
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
/**
Keeps n arrays concatenated.
Example:
```js
obj = Ember.Object.createWithMixins({
itemsA: [],
itemsB: [],
itemsC: [],
allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC');
});
obj.get('itemsA').pushObjects(['a', 'b']);
obj.get('allItems') //=> ['a', 'b']
obj.get('itemsB').pushObjects(['c']);
obj.get('allItems') //=> ['a', 'b', 'c']
obj.get('itemsC').pushObjects(['d']);
obj.get('allItems') //=> ['a', 'b', 'c', 'd']
obj.get('itemsB').pushObjects(['e', 'f']);
obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd']
```
@method concat
@for macros
@param *arguments Dependent keys with the arrays to concat.
@return {Array}
*/
export default function(...args) {
const arrayReduce = (...args) => {
return args.reduce((prev, value) => {
return prev.concat(value || []);
}, []);
};
return computed(...args.map((a) => normalizeArrayKey(a, [])), arrayReduce);
}