-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added @index for objects to #each helper. #451
Conversation
When traversing over an object in JavaScript, you're not guaranteed that the properties will be traversed in a particular (or even repeatable) order. The ECMA standard doesn't specify how object properties should be traversed—browsers might traverse them in order of their insertion or in numerical order, and there's variance between different browsers. What's your use case? |
The use case is the same as the use case when using an Array, and could include being able to know when it is the first or last item in the object. It just make sense that Array's have it, but Objects don't. |
There's no such thing of the "first" or "last" property of an object in JavaScript. Objects in JavaScript are, in a mathematical sense, sets of properties with no intrinsic order. How would you do this with pure JS? (and without writing 2 loops) |
|
var template = CompilerContext.compile(string); | ||
var result = template(hash); | ||
|
||
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is zero-based the proper thing here? Wouldn't the common use case be to display one-based numbering to the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to keep it consistent with arrays.
Handlebars doesn't parse @Index correctly. Any ideas why? |
+1 |
When iterating on objects, what is important is the My actual use case to display a clearfix for Bootstrap 3 every 4 items, to ensure a proper line return for various viewport size: <div class="row">
{{#each games}}
<div class="col-sm-3">
<h2><a href="/games/{{slug}}/">{{title}}</a></h2>
</div>
{{#eq @index 4}} <div class="clearfix"></div> {{/eq}}
{{/each}}
</div> With : var games = {
"barbie-super-model": {
"title": "Barbie Super Model",
"reviews_count": 1,
"rating": 0.5
},
"donkey-kong-country-2-diddy-s-kong-quest": {
"title": "Donkey Kong Country 2 : Diddy's Kong Quest",
"alternate_titles": [
"DKC2",
"DKC II"
],
"editor": "RARE",
"reviews_count": 1,
"rating": 5
}
} |
Implemented by #661 which was easier to merge. |
Currently the
@index
is only returned when looping through an array with the@each
helper. This PR also returns@index
when looping through objects as well.