@@ -16,7 +16,7 @@ export default class EachBlock extends Node {
16
16
iterations : string ;
17
17
index : string ;
18
18
context : string ;
19
- key : string ;
19
+ key : Expression ;
20
20
scope : TemplateScope ;
21
21
destructuredContexts : string [ ] ;
22
22
@@ -29,7 +29,10 @@ export default class EachBlock extends Node {
29
29
this . expression = new Expression ( compiler , this , scope , info . expression ) ;
30
30
this . context = info . context ;
31
31
this . index = info . index ;
32
- this . key = info . key ;
32
+
33
+ this . key = info . key
34
+ ? new Expression ( compiler , this , scope , info . key )
35
+ : null ;
33
36
34
37
this . scope = scope . child ( ) ;
35
38
@@ -262,7 +265,7 @@ export default class EachBlock extends Node {
262
265
mountOrIntro,
263
266
}
264
267
) {
265
- const key = block . getUniqueName ( 'key ' ) ;
268
+ const get_key = block . getUniqueName ( 'get_key ' ) ;
266
269
const blocks = block . getUniqueName ( `${ each } _blocks` ) ;
267
270
const lookup = block . getUniqueName ( `${ each } _lookup` ) ;
268
271
@@ -282,11 +285,14 @@ export default class EachBlock extends Node {
282
285
}
283
286
284
287
block . builders . init . addBlock ( deindent `
288
+ const ${ get_key } = ctx => ${ this . key . snippet } ;
289
+
285
290
for (var #i = 0; #i < ${ each_block_value } .${ length } ; #i += 1) {
286
- var ${ key } = ${ each_block_value } [#i].${ this . key } ;
287
- ${ blocks } [#i] = ${ lookup } [${ key } ] = ${ create_each_block } (#component, ${ key } , @assign(@assign({}, ctx), {
291
+ let child_ctx = @assign(@assign({}, ctx), {
288
292
${ this . contextProps . join ( ',\n' ) }
289
- }));
293
+ });
294
+ let key = ${ get_key } (child_ctx);
295
+ ${ blocks } [#i] = ${ lookup } [key] = ${ create_each_block } (#component, key, child_ctx);
290
296
}
291
297
` ) ;
292
298
@@ -313,7 +319,7 @@ export default class EachBlock extends Node {
313
319
block . builders . update . addBlock ( deindent `
314
320
var ${ each_block_value } = ${ snippet } ;
315
321
316
- ${ blocks } = @updateKeyedEach(${ blocks } , #component, changed, " ${ this . key } " , ${ dynamic ? '1' : '0' } , ${ each_block_value } , ${ lookup } , ${ updateMountNode } , ${ String ( this . block . hasOutroMethod ) } , ${ create_each_block } , "${ mountOrIntro } ", ${ anchor } , function(#i) {
322
+ ${ blocks } = @updateKeyedEach(${ blocks } , #component, changed, ${ get_key } , ${ dynamic ? '1' : '0' } , ${ each_block_value } , ${ lookup } , ${ updateMountNode } , ${ String ( this . block . hasOutroMethod ) } , ${ create_each_block } , "${ mountOrIntro } ", ${ anchor } , function(#i) {
317
323
return @assign(@assign({}, ctx), {
318
324
${ this . contextProps . join ( ',\n' ) }
319
325
});
0 commit comments