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

@index in Blaze #each #3716

Closed
wants to merge 9 commits into from
Closed

@index in Blaze #each #3716

wants to merge 9 commits into from

Conversation

Slava
Copy link
Contributor

@Slava Slava commented Feb 13, 2015

{{#each person in people}}
  <div>{{@index}}: {{person.name}}</div>
{{/each}}

wip

Regular #each ({{#each items}}) sets a new data context for each child view to
the item of the sequence.

The new #each ({{#each item in items}}) preserves the data context and extends
it to have the variable like 'item'
Remove the old implementation with modifying the data context,
implement it in the new way to keep the same data context but lookup
iterator in the scope.
@IstoraMandiri
Copy link

+1

Yep; useful handlebars functionality that's currently missing. Is it also present in template helper context?

@Slava
Copy link
Contributor Author

Slava commented Feb 13, 2015

@hitchcott no, I tried hard not to put it there. It is confusing that things like these are implemented in the data context in handlebars.js, I implemented them as local scoping that can be looked up within the template.

@@ -22,7 +22,7 @@ var unicodeClass = function (abbrev) {
// Match one or more characters that can start an identifier.
// This is IdentifierStart+.
var rIdentifierPrefix = new RegExp(
"^([a-zA-Z$_]+|\\\\u[0-9a-fA-F]{4}|" +
"^(@?[a-zA-Z$_]+|\\\\u[0-9a-fA-F]{4}|" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we support all kinds of identifiers with @ now? Or is there validation somewhere else which restricts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will change it to just accept '@Index' and nothing else starting with '@'

@Slava Slava closed this Feb 13, 2015
@Slava Slava deleted the each-index branch February 13, 2015 23:00
@Slava
Copy link
Contributor Author

Slava commented Feb 13, 2015

moving this to the each-in PR. I will address Sashko's comments there.

@mitar
Copy link
Contributor

mitar commented Feb 14, 2015

Where is "each-in PR"?

It would be great if this would be available inside a template helper. Then one could do a helper which returns something only for odd or even values.

@stubailo
Copy link
Contributor

You can always pass it as an argument. Each-in is here: #3560

@patmoore
Copy link
Contributor

Is this merged in? {{@Index}} in templates does not work.

@Slava
Copy link
Contributor Author

Slava commented Jul 31, 2015

@patmoore it has been merged long time ago, but it hasn't been release.

@patmoore
Copy link
Contributor

How frustrating! since it is in the docs.

@awatson1978
Copy link
Contributor

Gah. Just spent the last hour trying to figure out how to do this. Hope to see this in the 1.2 release....

@stubailo
Copy link
Contributor

@awatson1978 yep, it's going to be in 1.2!

@raix
Copy link
Contributor

raix commented Aug 11, 2015

👍

@yasaricli
Copy link

@Slava 👍 👍

@IstoraMandiri
Copy link

Wohoo

@derwaldgeist
Copy link

I wanted to use this since it was in the docs, but it failed. Now I read it's an error in the docs. Weird.
BTW: Will @Index be usable for object (hash-map) keys as well, or is it limited to arrays?

@jonlachlan
Copy link

Any way to access the index value from inside a helper?

Edit: I see your message above saying you tried to avoid this. I agree that keeping it out of the data context is good, however I wish it were stored somewhere in the template instance so that I could access it from helpers, events and onRendered.

@Slava
Copy link
Contributor Author

Slava commented Sep 3, 2015

@jonlachlan

just pass it to the helper, it is not that terrible:

{{#each person in people}}
  {{myHelper @index}}
{{/each}}
Template.bla.helpers({
  myHelper: function (index) {
    // do what you want
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.