Closed asakusuma closed 7 years ago
The default iteratorFor
implementation that glimmer-application uses, requires a key
to be specified (see here). Ember's version of {{each
does this for you automatically by utilizing Ember.guidFor
, but in a standalone glimmer environment you must manually specify the key to be used.
{{#each list key="id" as |item|}}
<li>{{item}}</li>
{{/each}}
Is id
a reserved word for the index of an array? I'm confused as to what a key is in the context of an array. Array items might have a key, but the entire array? I also don't see this key
concept in the handlebars docs.
For an array you would use {{#each list key="@index" as |item|}}
.
They key generally is a property on each item in the array, or as @robbiepitts mentioned you can use the special value "@index"
.
A similar concept exists in react land, and Ember just makes it slightly easier since it can use guidFor.
Yes, sorry, meant to say, "array of non-objects (like strings)".
Thanks for the responses. This feels clunky to me, as it seems like we are allowing the complexity of specific scenario (array of objects) to complicate the API for a simple scenario (array of primitives). Is this key
property documented anywhere? I don't see it on the handlebars built-in helpers page.
@asakusuma It's not documented anywhere because documentation for Glimmer doesn't exist yet. :) It is not surprising that it's not on the Handlebars page since this is a concern that only applies to diffing re-renders.
To close the loop here, the suggested syntax works. I'm fine closing this, but we may want to leave open as a reminder for something to highlight in the eventual glimmer docs.
@asakusuma Did I hit this well enough in https://www.glimmerjs.com/guides/templates-and-helpers ? See the bottom of the page ...
Closing for now, open an issue in the website repo for further discussion. Gracias :)
@acorncom yes, thanks!
A simple
each
loop doesn't seem to work.Repro commit to
hello-glimmer