index in each blocks

pull/31/head
Rich-Harris 9 years ago
parent 8c8b6ccfa4
commit 43c257b62e

@ -410,7 +410,7 @@ export default function generate ( parsed, template ) {
} }
const iteration = ${name}_iterations[i]; const iteration = ${name}_iterations[i];
${name}_iterations[i].update( ${current.contextChain.join( ', ' )}, ${expression}[i] ); ${name}_iterations[i].update( ${current.contextChain.join( ', ' )}, ${expression}[i]${node.index ? `, i` : ''} );
} }
for ( var i = ${expression}.length; i < ${name}_iterations.length; i += 1 ) { for ( var i = ${expression}.length; i < ${name}_iterations.length; i += 1 ) {
@ -425,18 +425,28 @@ export default function generate ( parsed, template ) {
for ( let i = 0; i < ${name}_iterations.length; i += 1 ) { for ( let i = 0; i < ${name}_iterations.length; i += 1 ) {
${name}_iterations[i].teardown(); ${name}_iterations[i].teardown();
} }
${name}_anchor.parentNode.removeChild( ${name}_anchor );
` ); ` );
const contexts = Object.assign( {}, current.contexts ); const contexts = Object.assign( {}, current.contexts );
const contextChain = current.contextChain.concat( node.context );
contexts[ node.context ] = true; contexts[ node.context ] = true;
if ( node.index ) {
// not strictly a context, but we can treat it as such
contextChain.push( node.index );
contexts[ node.index ] = true;
}
current = { current = {
useAnchor: false, useAnchor: false,
name: renderer, name: renderer,
target: 'target', target: 'target',
contexts, contexts,
contextChain: current.contextChain.concat( node.context ), contextChain,
initStatements: [], initStatements: [],
updateStatements: [], updateStatements: [],

@ -0,0 +1,6 @@
export default {
data: {
animals: [ 'adder', 'blue whale', 'chameleon' ]
},
html: `<p>0: adder</p><p>1: blue whale</p><p>2: chameleon</p><!--#each animals-->`
};

@ -0,0 +1,3 @@
{{#each animals as animal, i}}
<p>{{i}}: {{animal}}</p>
{{/each}}
Loading…
Cancel
Save