-
Notifications
You must be signed in to change notification settings - Fork 13
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
Scope and Context: Adding @first and @index to Object iteration #432
Comments
it's probably straightforward to do this as a helper now: registerHelper("first", function(options){
return options.scope.get("%index") === 0
}) /// this isn't going to perform great though
registerHelper("last", function(options){
return options.scope.get("%index") === scope.get("..").length
}) |
Awesome, thanks for the helper, I can certainly do that for now. Though is it also easy enough to add into the product? It's probably something useful that a lot of people would take advantage of, and considering that Handlebars implemented it 3 major versions ago it might be beneficial to keep the can-stache items up to par. |
Actually, after testing this, this still only works with Array's and not Objects. JS:
Stache:
Outcome:
With arrays it's fine, but we could already do |
Why do you loop through objects in this way? Looping through key-values has been 2nd-class priority for CanJS. Understanding use cases for this would help prioritize it. |
I have a component that I reuse in several different scenarios, all of which I send it a similar stack of data, but a lot of times they have different key names depending on the situation. For any given Object I send it, I need to know if this is the first one, because in this case I'm trying to add a If an |
While stache has
@key
/%key
/scope.key
when iterating over an object using{{#each...}}
, there are times when you don't want the key name, but the index iterator, as if it was an array. This is implemented in Handlebars v1.2.0 handlebars-lang/handlebars.js#661Description:
You have an object with named keys:
Using
scope.key
will returna
,b
,c
, ord
. However, If you don't know the names, or thata
is the first one, instead you want to understand the order and position of each item in the Object. Handlebars allows you to do the following:In the above scenario,
@first
would confirm it's the first item in the object, and@index
would print0
From http://handlebarsjs.com/builtin_helpers.html :
The text was updated successfully, but these errors were encountered: