Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array helper #318

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions text/0318-array-helper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
- Start Date: 2018-03-24
- RFC PR: https://github.com/emberjs/rfcs/pull/318
- Ember Issue: (leave this empty)

# `array` helper

## Summary

This RFC proposes to add an `array` template helper for creating arrays in templates.

The helper would be invoked as `(array arg1 ... argN)` and return the value `[arg1, ..., argN]`. For example, `(array 'a' 'b' 'c')` would return the value `['a', 'b', 'c']`.


## Motivation

Objects (or hashes) and arrays are the two main data structures in JavaScript. Ember already has a `hash` helper for building objects, so it makes sense to also include an `array` helper for building arrays.

## Detailed design

The design is straightforward and mirrors the design of the `hash` helper. In particular, the important thing to note is that if any of the arguments to the `array` helper change then an entirely new array will be returned, rather than updating the existing array in place.

The implementation would also mirror the [implementation of the `hash` helper](https://github.com/emberjs/ember.js/blob/ec9f4e5e5f4099a77a73bc5a9aa41916f0d15d6d/packages/ember-glimmer/lib/helpers/hash.ts#L49-L51) and would simply capture the positional arguments instead.

## How we teach this

This helper is not an important part of the programming model and can just be mentioned in the [API docs](https://emberjs.com/api/ember/release/classes/Ember.Templates.helpers) like its sibling the `hash` helper.

## Drawbacks

As usual, adding new helpers increases the surface area of the API and file size but in this case it is justified because the file size change is extremely small and its actually filling an existing hole in the API.

## Alternatives

This helper could be left to addons, and indeed there are addons that include this helper. It's also trivial to generate
your own `array` helper with `ember generate helper array`. Humorously, the default helper blueprint generates a helper that already acts like the `array` helper ;)

Nevertheless, I believe it's preferable to include this helper in Ember to fill the hole in Ember's API.