If I have a template instance that’s inside and #each, how can I get the current index? I say current because of course it can change as the context changes. For example:
Template.body.helpers({
children: [ 'A', 'B' ]
});
Template.child.onRendered(function(){
var templateInstance = this;
// Get current child index
});
Just to be clear I need to get the index out of the template instance because that’s all I have. I don’t have access to the collection used by the #each or the helper method.
well, that’s much more difficult… Only thing I can think of is iterate over the list of parent’s children and compare to self : http://meteorpad.com/pad/sthXDRPYnMsTykBSF/Child%20Template%20Index%202 Which I think is pretty inefficient, and I’m not sure exactly how the dom equality works ( like what if the elements are empty divs and have nothing unique in them), but the indexes are changing at least in the meteorpad example.
Inside the child :
var siblings = Template.instance().firstNode.parentElement.children;
_.each( siblings, function(sibling, index){
if( sibling===Template.instance().firstNode ){
// this is child template index
}
});