在聚合物飞镖中,我如何计算重复模板内部

Ala*_*rey 3 templates dart polymer dart-polymer

说我有一个重复的模板:

<template bind repeat id='my-template'>
    This is the bound value: <span id="#myid[x]"> {{}} </span>
</template>
Run Code Online (Sandbox Code Playgroud)

我能用这个替换[x]会有什么独特之处吗?访问循环计数器可以解决问题,但我愿意接受建议.

Jus*_*ani 5

我正在为Fancy Syntax(现在是Polymer.dart的默认绑定语法)添加一些实用程序来帮助解决这个问题,但基本的概述是通过一个过滤器运行你的集合,它将添加索引并返回一个新的Iterable.

这里有一些代码可以实现:

import 'package:fancy_syntax/fancy_syntax.dart';
import 'package:mdv/mdv.dart';

Iterable<IndexedValue> enumerate(Iterable iterable) {
  int i = 0;
  return iterable.map((e) => new IndexedValue(i++, e));
}

class IndexedValue<V> {
  final int index;
  final V value;

  IndexedValue(this.index, this.value);
}

main() {
  query('#my-template')
    ..bindingDelegate = new FancySyntax(globals: {
      'enumerate': enumerate,
    })
    ..model = ['A', 'B', 'C'];
}
Run Code Online (Sandbox Code Playgroud)
<template bind id='my-template'>
  <template repeat="{{ item in this | enumerate }}">
    This is the bound value: <span id="#myid-{{ item.index }}">{{ item.value }}</span>
  </template>
</template>
Run Code Online (Sandbox Code Playgroud)

我正在尝试将一堆像Python的itertools这样的实用程序放到一个库中,用于这样的用途.我会在它们可用时更新.