The {{collect}} helper takes a source object, a paths array and an
optional defaultValue, which is used if a given path cannot be found on the
source object. The defaultValue defaults to null.
{{collect source paths defaultValue="foo"}}
If the source is empty,
the helper will return an empty array ([]), regardless of the paths that
were specified.
If a specified path was not found on the source object, defaultValue is
put in its place.
const source = {
foo: 'ember',
bar: 'light',
qux: {
quax: 'table',
quuz: 'great'
}
};
const paths = ['bar', 'qux.quax', 'unknown', 'qux.quuz'];
{{#each (collect source paths defaultValue="is") as |word|}}
{{word}}
{{/each}}
light table is great
The specified paths on the source object are observed. This means that
updating these values on the source object will cause the helper to
recompute, just as you would expect.
You can also replace the source object altogether or change the specified
paths. Everything will always stay in sync.
To allow maximum flexibility, paths can also be a string, in which case the
value is returned as is and not wrapped in an array.
This means that the following two invocations have different return values:
const source = { foo: 'bar' };
const arrayPath = ['foo'];
const singularPath = 'foo';
{{collect source arrayPath}} => ['bar']
{{collect source singularPath}} => 'bar'
This is especially useful, when you are replacing {{get}} with
{{collect}}, but have some surrounding code that still expects the
unwrapped value for cases where paths is not an array, but also just a
single path, as it would be with {{get}}.
You can disable this behavior and make {{collect}} always return an array
by passing wrapSingular=true.
{{collect source "foo" wrapSingular=true}} => ['bar']
Properties
-
options
Collects the specified paths from the source object, if present.
Parameters:
Returns:
If the given source is present, returns an array of the
collected values in the order that they were specified in paths.
If the given source is empty, returns an empty array ([]).
If paths were not found in source, defaultValue will be inserted in
their place.
-
source -
paths -
options
Parameters:
-
sourceObjectThe object to collect values from
-
pathsString[][]The paths to collect
-
optionsObject
Returns:
If the given source is present, returns an array of the
collected values in the order that they were specified in paths.
If the given source is empty, returns an empty array ([]).
Called everytime an observed property on the source object changes.
Causes the helper to recomputed its value.
-
newPaths -
options
Updates the paths that are collected and observed for changes.
Also updates the observers on the source object.
Parameters:
-
newPathsString[][] -
[options]Object optional-
[isSingular=false]Boolean optional
-
Returns:
If the newPaths were actually different from the old
paths and thus an update was performed.