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

Template repeat index value is evaluated only after loop end #774

Closed
yogevyuval opened this issue Sep 27, 2014 · 1 comment
Closed

Template repeat index value is evaluated only after loop end #774

yogevyuval opened this issue Sep 27, 2014 · 1 comment

Comments

@yogevyuval
Copy link

I'm creating a list of items like this:

<template repeat="{{item in items}}">  
    <prog-bar
      name="{{item.name}}" >    
    </prog-bar>
</template

Now I want each prog-bar element to get also it's index in the list of items, So I add something like this:

<template repeat="{{item,i in items}}">  
    <prog-bar
      index="{{i}}"
      name="{{item.name}}" >    
    </prog-bar>
</template

Unfortunately, this does not work. When the attachedCallback function is called the value of this.index is undefined. By putting a breakpoint in attachedCallback() I see that the elements are created one after the other but the value of index is undefined, when all of the items are loaded then suddenly the index appear.

This led me to this solution:

<template repeat="{{item in items}}">  
    <prog-bar
      index="{{getIndex(item, items}}"
      name="{{item.name}}" >    
    </prog-bar>
</template

...

function getIndex(item, items) {
    return items.indexOf(item)
}

Am I missing something or is there a problem with evaluating the repeat index?

@ebidel
Copy link
Contributor

ebidel commented Sep 29, 2014

If you wait until domReady, index will be defined if you want to poke at them in JS. This waits an additional frame after attached. By that time, the binding system will have populated the value.

Try http://jsbin.com/metazabuzoji/1/edit

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

No branches or pull requests

2 participants