Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

template instances can't access their index position within the iteration #117

Closed
rafaelw opened this issue Jun 26, 2013 · 4 comments
Closed

Comments

@rafaelw
Copy link
Contributor

rafaelw commented Jun 26, 2013

e.g. <template repeat="{{ items }}">{{ _index }}</template>

cc @ebidel

@faassen
Copy link

faassen commented Jun 26, 2013

Just to throw a possibility into the hat. The approach I've used in Obviel Template (inspired by TAL templates) is to introduce a special "@repeat" variable, prefixed with @ to avoid name clashes.

@repeat.index gives you the index, @repeat.number is index + 1 (useful for UIs), I also have @repeat.length for the length of the array, and @repeat.first and @repeat.last are also useful (true only if at the beginning or end of the array).

If you have multiple scopes and want to explicitly refer to the index of the outer scope, I allow @repeat.outer.index. What happens when you have a scope called 'index'? I think I have this override the proper index, as you can always access the actual index using the full notation (@repeat.index.index, in this case).

Of course, @ would be a problem if you want the expression syntax to remain a subset of JS. You could avoid nameclashes by using a special object that is at least rare in JS.

@aeosynth
Copy link

+1

@ghost ghost assigned arv Oct 3, 2013
@gavindoughtie
Copy link

+1 (my example is a fan of playing cards, where each one has a slightly different rotation value)

@rafaelw
Copy link
Contributor Author

rafaelw commented Oct 16, 2013

Fixed: googlearchive/polymer-expressions@b1fc58f

e.g.

<template repeat="{{ user, userIndex in users }}">
  Index Position: {{ userIndex }}, Username: {{ user.name }}
</template>

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

No branches or pull requests

5 participants